Part Number Hot Search : 
74ABT A1225UA4 IXTA110N AOZ1253 TFP4N60 BU210 D45H4 SNXXX
Product Description
Full Text Search
 

To Download ATTINY10 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  features ? high performance, low power avr ? 8-bit microcontroller ? advanced risc architecture ? 54 powerful instructions ? most single clock cycle execution ? 16 x 8 general purpose working registers ? fully static operation ? up to 12 mips throughput at 12 mhz ? non-volatile program and data memories ? 512/1024 bytes of in-system programmable flash program memory ? 32 bytes internal sram ? flash write/erase cycles: 10,000 ? data retention: 20 years at 85 o c / 100 years at 25 o c ? peripheral features ? one 16-bit timer/counter with prescaler and two pwm channels ? programmable watchdog timer wi th separate on-chip oscillator ? 4-channel, 8-bit analog to digital converter (1) ? on-chip analog comparator ? special microcontroller features ? in-system programmable (2) ? external and internal interrupt sources ? low power idle, adc noise redu ction, and powe r-down modes ? enhanced power-on reset circuit ? programmable supply voltage level monitor with interrupt and reset ? internal calibrated oscillator ? i/o and packages ? 6-pin sot: four programmable i/o lines ? operating voltage: ? 1.8 ? 5.5v ? programming voltage: ?5v ? speed grade ? 0 ? 4 mhz @ 1.8 ? 5.5v ? 0 ? 8 mhz @ 2.7 ? 5.5v ? 0 ? 12 mhz @ 4.5 ? 5.5v ? industrial temperature range ? low power consumption ? active mode: ? 200a at 1mhz and 1.8v ?idle mode: ? 25a at 1mhz and 1.8v ? power-down mode: ? < 0.1a at 1.8v note: 1. the analog to digital converter (adc) is available in attiny5/10, only 2. at 5v, only 8-bit microcontroller with 512/1024 bytes in-system programmable flash attiny4/5/9/10 preliminary 8127c?avr?10/09
2 8127c?avr?10/09 attiny4/5/9/10 1. pin configurations figure 1-1. pinout of attiny4/5/9/10 1.1 pin description 1.1.1 vcc supply voltage. 1.1.2 gnd ground. 1.1.3 port b (pb3..pb0) this is a 4-bit, bi-directional i/o port with inter nal pull-up resistors, in dividually selectable for each bit. the output buffers have symmetrical dr ive characteristics, with both high sink and source capability. as inputs, the port pins that are externally pulled low will source curr ent if pull- up resistors are activated. port pins are tri-st ated when a reset condition becomes active, even if the clock is not running. the port also serves the function s of various special features of the attiny4/5/9/10, as listed on page 36 . 1.1.4 reset reset input. a low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running and prov ided the reset pin has not been disabled. the min- imum pulse length is given in table 16-4 on page 119 . shorter pulses are not guaranteed to generate a reset. the reset pin can also be used as a (weak) i/o pin. 1 2 3 6 5 4 (pcint0/tpidata/oc0a/adc0/ain0) pb0 gnd (pcint1/tpiclk/clki/icp0/oc0b/adc1/ain1) pb1 pb3 (reset/pcint3/adc3) vcc pb2 (t0/clko/pcint2/int0/adc2) sot-23
3 8127c?avr?10/09 attiny4/5/9/10 2. overview attiny4/5/9/10 are low-power cmos 8-bit microcontrollers based on the compact avr enhanced risc architecture. by executing powerful instructions in a single clock cycle, the attiny4/5/9/10 achieve throu ghputs approaching 1 mips per mh z, allowing the system designer to optimize power consumption versus processing speed. figure 2-1. block diagram the avr core combines a rich instruction set with 16 general purpose working registers and system registers. all registers are directly connect ed to the arithmetic lo gic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is compact and code efficient whil e achieving throughputs up to ten times faster than conventional cisc microcontrollers. stack pointer sram program counter programming logic isp interface internal oscillator watchdog timer reset flag register mcu status register timer/ counter0 calibrated oscillator timing and control interrupt unit analog comparator adc general purpose registers x y z alu status register program flash instruction register instruction decoder control lines v cc reset data register port b direction reg. port b drivers port b gnd pb3:0 8-bit data bus
4 8127c?avr?10/09 attiny4/5/9/10 the attiny4/5/9/10 provide the following f eatures: 512/1024 byte of in-system programmable flash, 32 bytes of sram, four general purpose i/o lines, 16 general purpose working registers, a 16-bit timer/counter with two pwm channels, internal and external interrupts, a programmable watchdog timer with internal oscilla tor, an internal calibrated oscilla tor, and four software select- able power saving modes. attiny5/10 are also equipped with a four-channel, 8-bit analog to digital converter (adc). idle mode stops the cpu while allowing the sram , timer/counter, adc (attiny5/10, only), ana- log comparator, and interrupt system to continue functioning. adc noise reduction mode minimizes switching noise during adc conversi ons by stopping the cpu and all i/o modules except the adc. in power-down mode registers keep their contents and all chip functions are disabled until the next interrupt or hardware rese t. in standby mode, the oscillator is running while the rest of the device is sleeping, allo wing very fast start-up combined with low power consumption. the device is manufactured using atmel?s high density non-volatile memory technology. the on- chip, in-system programmable flash allows program memory to be re-programmed in-system by a conventional, non-volatile memory programmer. the attiny4/5/9/10 avr are supported by a su ite of program and system development tools, including macro assemblers and evaluation kits. 2.1 comparison of attiny4, at tiny5, attiny9 and ATTINY10 a comparison of the devices is shown in table 2-1 . table 2-1. differences between attiny4, attiny5, attiny9 and ATTINY10 device flash adc signature attiny4 512 bytes no 0x1e 0x8f 0x0a attiny5 512 bytes yes 0x1e 0x8f 0x09 attiny9 1024 bytes no 0x1e 0x90 0x08 ATTINY10 1024 bytes yes 0x1e 0x90 0x03
5 8127c?avr?10/09 attiny4/5/9/10 3. general information 3.1 resources a comprehensive set of drivers, application notes, data sheets and descriptions on development tools are available for downloa d at http://www.a tmel.com/avr. 3.2 code examples this documentation contains simple code examples that briefly show how to use various parts of the device. these code examples assume that the part specific header file is included before compilation. be aware that not all c compiler vendors include bit definitions in the header files and interrupt handling in c is compiler dependent. please confirm with the c compiler documen- tation for more details. 3.3 data retention reliability qualification results sh ow that the projected data retention failure rate is much less than 1 ppm over 20 years at 85c or 100 years at 25c. 3.4 disclaimer typical values contained in th is datasheet are based on simula tions and characterization of other avr microcontrollers manufactured on th e same process technology. min and max values will be available after the devi ce has been characterized.
6 8127c?avr?10/09 attiny4/5/9/10 4. cpu core this section discusses the avr core architecture in general. the main function of the cpu core is to ensure correct program execution. the cpu must therefore be able to access memories, perform calculations, control peri pherals, and handle interrupts. 4.1 architectural overview figure 4-1. block diagram of the avr architecture in order to maximize performance and parallelism, the avr uses a harvard architecture ? with separate memories and buses for program and data. instructions in the program memory are executed with a single level pipe lining. while one instruction is being executed, the next instruc- tion is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is in-system reprogrammable flash memory. the fast-access register file contains 16 x 8-bit general purpose working registers with a single clock cycle access time. this allows single-cycle ar ithmetic logic unit (alu ) operation. in a typ- ical alu operation, two operands are output from the register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. flash program memory instruction register instruction decoder program counter control lines 16 x 8 general purpose registrers alu status and con trol i/o lines data bus 8-bit data sra m direct addressing indirect addressing interrupt unit watchdog timer analog comparator timer/counter 0 adc
7 8127c?avr?10/09 attiny4/5/9/10 six of the 16 registers can be used as three 16- bit indirect address register pointers for data space addressing ? enabling efficient address ca lculations. one of the these address pointers can also be used as an address pointer for look up tables in flash program memory. these added function registers are the 16-bit x-, y-, and z-register, described later in this section. the alu supports arithmetic and logic operations between registers or between a constant and a register. single register operations can also be executed in the alu. after an arithmetic opera- tion, the status register is up dated to reflect information about the result of the operation. program flow is provided by conditional and un conditional jump and call instructions, capable of directly addressing the whole address space. most avr instructions have a single 16-bit word format but 32-bit wide instructions also exist. th e actual instruction set varies, as some devices only implement a part of the instruction set. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in th e general data sram, and consequently the stack size is only limited by the sram size and the us age of the sram. all user programs must initial- ize the sp in the reset routine (before subroutines or interrupts are executed). the stack pointer (sp) is read/write accessible in the i/o space. the data sram can easily be accessed through the four different addressing modes supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps. a flexible interrupt module has its control r egisters in the i/o spac e with an additional global interrupt enable bit in the status register. all interrupts have a s eparate interrupt vector in the interrupt vector table. the interrupts have priority in accordance with their interrupt vector posi- tion. the lower the interr upt vector address, the higher the priority. the i/o memory space contains 64 addresses for cpu peripheral functi ons as control regis- ters, spi, and other i/o functions. the i/o memory can be accessed as the data space locations, 0x0000 - 0x003f. 4.2 alu ? arithm etic logic unit the high-performance avr alu operates in dire ct connection with all the 16 general purpose working registers. within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immedi ate are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-functions. some implementations of the architecture also provide a powerful multip lier supporting both signed/unsigned multiplication and fractional format. see document ?avr instruction set? and section ?instruction set sum- mary? on page 151 for a detailed description. 4.3 status register the status register contains information about th e result of the most recently executed arithme- tic instruction. this information can be used for altering program flow in order to perform conditional operations. note that the status re gister is updated after all alu operations, as specified in document ?avr instruction set? and section ?instruction set summary? on page 151 . this will in many cases remove the need fo r using the dedicated compare instructions, resulting in faster and more compact code. the status register is not automatically st ored when entering an interrupt routine and restored when returning from an interrupt. this must be handled by software.
8 8127c?avr?10/09 attiny4/5/9/10 4.4 general purpose register file the register file is optimized for the avr enhanc ed risc instruction set. in order to achieve the required performance and flex ibility, the following in put/output schemes ar e supported by the register file: ? one 8-bit output operand and one 8-bit result input ? two 8-bit output operands and one 8-bit result input ? one 16-bit output operand and one 16-bit result input figure 4-2 below shows the structure of the 16 general purpose working registers in the cpu. figure 4-2. avr cpu general purpose working registers note: a typical implementation of the avr register file includes 32 general prupose registers but attiny4/5/9/10 implement only 16 registers. fo r reasons of compatibilit y the registers are num- bered r16...r31, not r0...r15. most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. 4.4.1 the x-register, y-register, and z-register registers r26..r31 have some added functions to their general purpose usage. these registers are 16-bit address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as described in figure 4-3 . 70 r16 r17 general r18 purpose ? working r26 x-register low byte registers r27 x-register high byte r28 y-register low byte r29 y-register high byte r30 z-register low byte r31 z-register high byte
9 8127c?avr?10/09 attiny4/5/9/10 figure 4-3. the x-, y-, and z-registers in different addressing modes these address r egisters function as automatic increment and automatic decrement (see document ?avr instruction set? and section ?instruction set sum- mary? on page 151 for details). 4.5 stack pointer the stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. the stack pointer register always points to the top of the stack. note that the stack is implemented as growing from higher memory loca- tions to lower memory location s. this implies that a stack push command decreases the stack pointer. the stack pointer points to the data sram stack area where the subroutine and interrupt stacks are located. this stack space in the da ta sram must be defined by the program before any subroutine calls are executed or interrupt s are enabled. the stack pointer must be set to point above 0x40. the stack pointer is decrement ed by one when data is pushed onto the stack with the push instruction, and it is decremented by two when the return address is pushed onto the stack with subroutine call or interrupt. the st ack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when data is popped from the stack with return from subr outine ret or return from interrupt reti. the avr stack pointer is implemented as two 8- bit registers in the i/o space. the number of bits actually used is implementation dependent. no te that the data space in some implementa- tions of the avr architecture is so small that only spl is needed. in this case, the sph register will not be present. 4.6 instruction execution timing this section describes the general access timing concepts for instructi on execution. the avr cpu is driven by the cpu clock clk cpu , directly generated from the selected clock source for the chip. no internal clo ck division is used. 15 xh xl 0 x-register 707 0 r27 r26 15 yh yl 0 y-register 707 0 r29 r28 15 zh zl 0 z-register 707 0 r31 r30
10 8127c?avr?10/09 attiny4/5/9/10 figure 4-4. the parallel instruction fetche s and instruction executions figure 4-4 shows the parallel instruction fetches and instruction executions enabled by the har- vard architecture and the fast access register f ile concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the corr esponding unique results for functions per cost, functions per clocks, and functions per power-unit. figure 4-5 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destina- tion register. figure 4-5. single cycle alu operation 4.7 reset and in terrupt handling the avr provides several different interrupt sources. these interrupts and the separate reset vector each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits which must be writ ten logic one together with the global interrupt enable bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are by default defined as the reset and interrupt vectors. the complete list of vectors is shown in ?interrupts? on page 35 . the list also determines the priority levels of the different interrupts. the lower the address the higher is the priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0. when an interrupt occurs, the global interrupt enable i-bit is cleared and all interrupts are dis- abled. the user software can write logic one to the i-bit to enable nested interrupts. all enabled clk 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 cpu total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 clk cpu
11 8127c?avr?10/09 attiny4/5/9/10 interrupts can then interrupt the current interrupt routine. the i-bit is automatically set when a return from interrupt instru ction ? reti ? is executed. there are basically two types of interrupts. the fi rst type is triggered by an event that sets the interrupt flag. for these interrupts, the program co unter is vectored to th e actual interrupt vec- tor in order to execute the interrupt handli ng routine, and hardware clears the corresponding interrupt flag. interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the interrupt fl ag will be set and remember ed until the interrupt is enabled, or the flag is cleared by software. similarly, if one or more interrupt conditions occur while the global interrupt enable bit is cleared, the corr esponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set, and will then be exec uted by order of priority. the second type of interrupts will trigger as long as the interrupt condition is present. these interrupts do not necessarily have interrupt flags. if the interrupt condition disappears before the interrupt is enabled, the in terrupt will not be triggered. when the avr exits from an inte rrupt, it will always retu rn to the main pr ogram and execute one more instruction before any pending interrupt is served. note that the status register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. this must be handled by software. when using the cli instruction to disable interrupts, the interrup ts will be immediately disabled. no interrupt will be ex ecuted after the cli instru ction, even if it occurs simultaneously with the cli instruction. when using the sei instruction to enable interr upts, the instruction following sei will be exe- cuted before any pending interrupts, as shown in the following example. note: see ?code examples? on page 5 . 4.7.1 interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles mini- mum. after four clock cycles the program vector address for the actual interrupt handling routine is executed. during this four clock cycle perio d, the program counter is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi- cycle instruction, this in struction is completed before the interrupt is served. if an interrupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clo ck cycles. this increase co mes in addition to the start-up time from the selected sleep mode. a return from an interrupt handling routine take s four clock cycles. during these four clock cycles, the program counter (two bytes) is po pped back from the stack, the stack pointer is incremented by two, and the i-bit in sreg is set. assembly code example sei ; set global interrupt enable sleep ; enter sleep, waiting for interrupt ; note: will enter sleep before any pending interrupt(s)
12 8127c?avr?10/09 attiny4/5/9/10 4.8 register description 4.8.1 ccp ? configuration change protection register ? bits 7:0 ? ccp[7:0] ? configuration change protection in order to change the contents of a protected i/ o register the ccp register must first be written with the correct signature. after ccp is written the protected i/o registers may be written to dur- ing the next four cpu instruction cycles. all in terrupts are ignored during these cycles. after these cycles interrupts are autom atically handled again by the cpu, and any pending interrupts will be executed according to their priority. when the protected i/o re gister signature is writ ten, ccp[0] will read as on e as long as the pro- tected feature is enab led, while ccp[7:1] will always read as zero. table 4-1 shows the signatures that are in recognised. 4.8.2 sph and spl ? stack pointer register 4.8.3 sreg ? status register ? bit 7 ? i: global interrupt enable the global interrupt enable bit must be set for th e interrupts to be enabled. the individual inter- rupt enable control is then performed in separate control registers. if the global interrupt enable register is cleared, none of t he interrupts are enabled independent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interr upts. the i-bit can also be set and cleared by the application with the sei and cli instructions, as described in the document ?avr instruction set? and ?instruction set summary? on page 151 . bit 76543210 0x3c ccp[7:0] ccp read/write wwwwwwww initial value00000000 table 4-1. signatures recognised by the confi guration change protection register signature group description 0xd8 ioreg: clkmsr, clkpsr, wdtcsr protected i/o register bit 151413121110 9 8 0x3e sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 sph 0x3d sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value ramend ramend ramend ramend ramend ramend ramend ramend initial value ramend ramend ramend ramend ramend ramend ramend ramend bit 76543210 0x3f i t h s v n z c sreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
13 8127c?avr?10/09 attiny4/5/9/10 ? bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (b it store) use the t-bi t as source or desti- nation for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction. ? bit 5 ? h: half carry flag the half carry flag h indicates a half carry in some arithmetic operation s. half carry is useful in bcd arithmetic. see document ?avr instruction set? and section ?instruction set summary? on page 151 for detailed information. ? bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s complement overflow flag v. see document ?avr instruction set? and section ?instruction set summary? on page 151 for detailed information. ? bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v supports two?s complement arithmetics. see document ?avr instruction set? and section ?instruction set summary? on page 151 for detailed information. ? bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see docu- ment ?avr instruction set? and section ?instruction set summary? on page 151 for detailed information. ? bit 1 ? z: zero flag the zero flag z indicates a zero result in an arithmetic or logic operation. see document ?avr instruction set? and section ?instruction set summary? on page 151 for detailed information. ? bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see document ?avr instruction set? and section ?instruction set summary? on page 151 for detailed information.
14 8127c?avr?10/09 attiny4/5/9/10 5. memories this section describes the different memories in the attiny4/5/9/10. devices have two main memory areas, the program memory space and the data memory space. 5.1 in-system re-programma ble flash program memory the attiny4/5/9/10 contain 512/1024 bytes of on-chip, in-system reprogrammable flash mem- ory for program storage. since all avr instructions are 16 or 32 bits wide, the flash is organized as 256/512 x 16. the flash memory has an endurance of at leas t 10,000 write/erase cycle s. the attiny4/5/9/10 program counter (pc) is 9 bits wide, thus c apable of addressing the 256/512 program memory locations, starting at 0x000. ?memory programming? on page 107 contains a detailed description on flash data serial downloading. constant tables can be allocated within the enti re address space of program memory. since pro- gram memory can not be accessed directly, it has been mapped to the data memory. the mapped program memory begins at byte address 0x4000 in data memory (see figure 5-1 on page 15 ). although programs are exec uted starting from address 0x000 in program memory it must be addressed starting from 0x4000 when accessed via the data memory. internal write operations to flash program memory have been disabled and program memory therefore appears to firmware as read-only. flash memory can still be written to externally but internal write operations to the program memory ar ea will not be succesful. timing diagrams of instruction fetch and execution are presented in ?instruction execution tim- ing? on page 9 . 5.2 data memory data memory locations include the i/o memory, the internal sram memory, the non-volatile memory lock bits, and the flash memory. see figure 5-1 on page 15 for an illustration on how the attiny4/5/9/10 memory space is organized. the first 64 locations are reserved for i/o memory, while the following 32 data memory locations address the internal data sram. the non-volatile memory lock bits and all the flash memory sections are mapped to the data memory space. these locations appear as read-only for device firmware. the four different addressing modes for data memo ry are direct, indirect, indirect with pre-decre- ment, and indirect with post-increment. in the register file, registers r26 to r31 function as pointer registers for indirect addressing. the in and out instructions can access all 64 lo cations of i/o memory. direct addressing using the lds and sts instructions reaches the 128 locations between 0x0040 and 0x00bf. the indirect addressing reaches t he entire data memory space. when using indirect addressing modes with automatic pre-decremen t and post-increment, the address registers x, y, and z are decremented or incremented.
15 8127c?avr?10/09 attiny4/5/9/10 figure 5-1. data memory map (byte addressing) 5.2.1 data memory access times this section describes the general access timi ng concepts for internal memory access. the internal data sram access is performed in two clk cpu cycles as described in figure 5-2 . figure 5-2. on-chip data sram access cycles 0x0000 ... 0x003f 0x0040 ... 0x005f 0x0060 ... 0x3eff 0x3f00 ... 0x3f01 0x3f02 ... 0x3f3f 0x3f40 ... 0x3f41 0x3f42 ... 0x3f7f 0x3f80 ... 0x3f81 0x3f82 ... 0x3fbf 0x3fc0 ... 0x3fc3 0x3fc4 ... 0x3fff 0x4000 ... 0x41ff/0x43ff 0x4400 ... 0xffff i/o space sram data memory (reserved) nvm lock bits (reserved) configuration bits (reserved) calibration bits (reserved) device id bits (reserved) flash program memory (reserved) clk wr rd data data address address valid t1 t2 t3 compute address read write cpu memory access instruction next instruction
16 8127c?avr?10/09 attiny4/5/9/10 5.3 i/o memory the i/o space definition of the attiny4/5/9/10 is shown in ?register summary? on page 149 . all attiny4/5/9/10 i/os and peripherals are pl aced in the i/o space. all i/o locations may be accessed using the ld and st instructions, enab ling data transfer between the 16 general pur- pose working registers and the i/o space. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi in structions. in these registers, the value of sin- gle bits can be checked by using the sb is and sbic instructions. see document ?avr instruction set? and section ?instruction set summary? on page 151 for more details. when using the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. some of the status flags are cleared by writi ng a logical one to them. note that cbi and sbi instructions will only oper ate on the specified bit, and can th erefore be used on registers contain- ing such status flags. the cbi and sbi instructi ons work on registers in the address range 0x00 to 0x1f, only. the i/o and peripherals control registers are explained in later sections.
17 8127c?avr?10/09 attiny4/5/9/10 6. clock system figure 6-1 presents the principal clock systems and their distribution in attiny4/5/9/10. all of the clocks need not be active at a given time. in order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes and power reduction reg- ister bits, as described in ?power management and sleep modes? on page 23 . the clock systems is detailed below. figure 6-1. clock distribution 6.1 clock subsystems the clock subsystems are detailed in the sections below. 6.1.1 cpu clock ? clk cpu the cpu clock is routed to parts of the syst em concerned with operat ion of the avr core. examples of such modules are the general purpose register file, the system registers and the sram data memory. halting th e cpu clock inhibits the core from performing general opera- tions and calculations. 6.1.2 i/o clock ? clk i/o the i/o clock is used by the majority of the i/o modules, like timer/counter. the i/o clock is also used by the external interrupt module, but note that some external interrupts are detected by asynchronous logic, allowing such interrupts to be detected even if the i/o clock is halted. 6.1.3 nvm clock - clk nvm the nvm clock controls operation of the non-vola tile memory controller. the nvm clock is usu- ally active simultane ously with the cpu clock. clock control unit general i/o modules analog-to-digital converter cpu core watchdog timer reset logic clock prescaler ram clock switch nvm calibrated oscillator clk adc source clock clk i/o clk cpu clk nvm watchdog clock watchdog oscillator external clock
18 8127c?avr?10/09 attiny4/5/9/10 6.1.4 adc clock ? clk adc the adc is provided with a dedicated clock domain. this allows halting the cpu and i/o clocks in order to reduce noise generated by digital circ uitry. this gives more accurate adc conversion results. the adc is available in attiny5/10, only. 6.2 clock sources all synchronous clock signals are derived from the main clock. the device has three alternative sources for the main clock, as follows: ? calibrated internal 8 mhz oscillator (see page 18 ) ? external clock (see page 18 ) ? internal 128 khz oscillator (see page 19 ) see table 6-3 on page 21 on how to select and change the active clock source. 6.2.1 calibrated intern al 8 mhz oscillator the calibrated internal oscillato r provides an approximately 8 mhz clock signal. though voltage and temperature dependent, this clock can be very accurately calibrated by the user. see table 16-2 on page 118 , figure 17-39 on page 142 and figure 17-40 on page 142 for more details. this clock may be selected as the main clock by setting the clock main select bits clkms[1:0] in clkmsr to 0b00. once enabled, the oscilla tor will operate with no ex ternal components. dur- ing reset, hardware loads the calibration byte into the osccal register and thereby automatically calibrates the oscilla tor. the accuracy of this calibra tion is shown as factory cali- bration in table 16-2 on page 118 . when this oscillator is used as the main clock, the watchdog osci llator will still be used for the watchdog timer and reset time-out. for more information on the pre-programmed calibration value, see section ?calibration section? on page 110 . 6.2.2 external clock to use the device with an external clock so urce, clki should be driven as shown in figure 6-2 . the external clock is selected as the main clock by setting clkms[1:0] bits in clkmsr to 0b10. figure 6-2. external clock drive configuration when applying an external clock, it is required to avoid sudden changes in the applied clock fre- quency to ensure stable operation of the mcu. a variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable behavior. it is required to en sure that the mcu is kept in reset during such changes in the clock frequency. external clock s ignal clki gnd
19 8127c?avr?10/09 attiny4/5/9/10 6.2.3 internal 128 khz oscillator the internal 128 khz oscillator is a low power oscillator providing a clock of 128 khz. the fre- quency depends on supply voltage, temperature and batch variations. this clock may be select as the main clock by setting the cl kms[1:0] bits in clkmsr to 0b01. 6.2.4 switching clock source the main clock source can be switched at run-time using the ?clkmsr ? clock main settings register? on page 21 . when switching between any clock sources, the clock system ensures that no glitch occurs in the main clock. 6.2.5 default clock source the calibrated internal 8 mhz oscillator is always selected as main clock when the device is powered up or has been reset. the synchronous sy stem clock is the main clock divided by 8, controlled by the system clock pres caler. the clock prescaler select bits can be written later to change the system clock frequency. see ?system clock prescaler?. 6.3 system clock prescaler the system clock is derived from the main clock via the system clock prescaler. the system clock can be divided by setting the ?clkpsr ? clock prescale register? on page 22 . the sys- tem clock prescaler can be used to decrease pow er consumption at ti mes when requirements for processing power is low or to bring the syst em clock within limits of maximum frequency. the prescaler can be used with all ma in clock source options, and it will affect the cl ock frequency of the cpu and all synchronous peripherals. the system clock prescaler can be used to impl ement run-time changes of the internal clock frequency while still ensur ing stable operation. 6.3.1 switching prescaler setting when switching between prescaler settings, the system clock pr escaler ensures that no glitch occurs in the system clock and that no interme diate frequency is higher than neither the clock frequency corresponding the previous setting, nor the clock frequency corresponding to the new setting. the ripple counter that implements the prescaler runs at the frequency of the main clock, which may be faster than the cpu's clock frequency. henc e, it is not possible to determine the state of the prescaler - even if it were readable, and the ex act time it takes to switch from one clock divi- sion to another cannot be exactly predicted. from the time the clkps values ar e written, it takes between t1 + t2 and t1 + 2*t2 before the new clock frequency is active. in this interval, two active clock edges are produced. here, t1 is the previous clock period, and t2 is the perio d corresponding to the new prescaler setting.
20 8127c?avr?10/09 attiny4/5/9/10 6.4 starting 6.4.1 starting from reset the internal reset is immediately asserted when a reset source goes active. the internal reset is kept asserted until the reset source is released and the start-up sequence is completed. the start-up sequence includes three steps, as follows. 1. the first step after the reset source has been released consists of the device counting the reset start-up time . the purpose of this reset start- up time is to ensure that supply voltage has reached sufficient levels. the rese t start-up time is counted using the inter- nal 128 khz oscillator. see table 6-1 for details of reset start-up time. note that the actual supply voltage is not monitored by the start-up logic. the device will count until the reset start-up time has el apsed even if the devic e has reached suffi- cient supply voltage levels earlier. 2. the second step is to count the oscillator start-up time, which ensures that the cali- brated internal oscillator has re ached a stable state before it is used by the other parts of the system. the calibrated internal oscillator needs to oscillate for a minimum num- ber of cycles before it can be considered stable. see table 6-1 for details of the oscillator star t-up time. 3. the last step before releasing the internal reset is to load the calibration and the config- uration values from the non- volatile memory to configure the device properly. the configuration time is listed in table 6-1 . notes: 1. after powering up the device or after a rese t the system clock is auto matically set to calibrated internal 8 mhz oscillator, divided by 8 6.4.2 starting from power-down mode when waking up from po wer-down sleep mode, the supply voltage is assumed to be at a suffi- cient level and only the o scillator start-up time is counted to ensure the stable operation of the oscillator. the oscillator start-up time is count ed on the selected main clock, and the start-up time depends on the clock selected. see table 6-2 for details. notes: 1. the start-up time is measur ed in main clock oscillator cycles. 6.4.3 starting from idle / adc noise reduction / standby mode when waking up from idle, adc noise reduction or standby mode, the oscillator is already run- ning and no oscillator start-up time is introduced. the adc is available in attiny5/10, only. table 6-1. start-up times when using the internal calibrated oscillator reset oscillator configuration total start-up time 64 ms 6 cycles 21 cycles 64 ms + 6 oscilla tor cycles + 21 system clock cycles (1) table 6-2. start-up time from power-down sleep mode. oscillator start-up time total start-up time 6 cycles 6 oscillator cycles (1)
21 8127c?avr?10/09 attiny4/5/9/10 6.5 register description 6.5.1 clkmsr ? clock main settings register ? bit 7:2 ? res: reserved bits these bits are reserved and always read zero. ? bit 1:0 ? clkms[1:0]: clock main select bits these bits select the main clock source of the system. the bits can be written at run-time to switch the source of the main clock. the clock system ensures glitch fr ee switching of the main clock source. the main clock alternatives are shown in table 6-3 . to avoid unintentional switching of main clock source, a protected change sequence must be followed to change the clkms bits, as follows: 1. write the signature for change enable of protected i/o register to register ccp 2. within four instruction cycles, write the clkms bits with the desired value 6.5.2 osccal ? oscillato r calibration register . ? bits 7:0 ? cal[7:0]: oscillator calibration value the oscillator calibration register is used to trim the calibrated in ternal oscillator and remove pro- cess variations from the oscillator fr equency. a pre-programmed calibration value is automatically written to this register during ch ip reset, giving the factory calibrated frequency as specified in table 16-2, ?calibration accuracy of internal rc oscillator,? on page 118 . the application software can write this register to change the o scillator frequency. the oscillator can be calibrated to frequencies as specified in table 16-2, ?calibration accuracy of internal rc oscillator,? on page 118 . calibration outside the range given is not guaranteed. the cal[7:0] bits are used to tu ne the frequency of the oscillator. a setting of 0x00 gives the lowest frequency, and a setting of 0xff gives the highest frequency. bit 765432 1 0 0x37 ? ? ? ? ? ? clkms1 clkms0 clkmsr read/writerrrrrrr/wr/w initial value 0 0 0 0 0 0 0 0 table 6-3. selection of main clock clkm1 clkm0 main clock source 0 0 calibrated internal 8 mhzoscillator 0 1 internal 128 khz oscillator (wdt oscillator) 1 0 external clock 11reserved bit 76543210 0x39 cal7 cal6 cal5 cal4 cal3 cal2 cal1 cal0 osccal read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
22 8127c?avr?10/09 attiny4/5/9/10 6.5.3 clkpsr ? clock prescale register ? bits 7:4 ? res: reserved bits these bits are reserved and will always read as zero. ? bits 3:0 ? clkps[3:0]: clock prescaler select bits 3 - 0 these bits define the division factor between th e selected clock source and the internal system clock. these bits can be written at run-time to vary the clock frequency and suit the application requirements. as the prescaler di vides the master clock input to the mcu, the speed of all syn- chronous peripherals is reduced accordi ngly. the division factors are given in table 6-4 . to avoid unintentional changes of clock freque ncy, a protected change sequence must be fol- lowed to change the clkps bits: 1. write the signature for change enable of protected i/o register to register ccp 2. within four instruction cycles, write the desired value to clkps bits at start-up, clkps bits are reset to 0b0011 to sele ct the clock division fact or of 8. if the selected clock source has a frequency higher than the maximum allowed the application software must make sure a sufficient division factor is used. to make sure the write procedure is not inter- rupted, interrupts must be disabled when changing prescaler settings. bit 76543210 0x36 ? ? ? ? clkps3 clkps2 clkps1 clkps0 clkpsr read/write r r r r r/w r/w r/w r/w initial value00000011 table 6-4. clock prescaler select clkps3 clkps2 clkps1 clkps0 clock division factor 0000 1 0001 2 0010 4 0 0 1 1 8 (default) 0100 16 0101 32 0110 64 0 1 1 1 128 1 0 0 0 256 1001 reserved 1010 reserved 1011 reserved 1100 reserved 1101 reserved 1110 reserved 1111 reserved
23 8127c?avr?10/09 attiny4/5/9/10 7. power management and sleep modes the high performance and industry leading code ef ficiency makes the avr microcontrollers an ideal choise for low power applications. in addition, sleep modes enable the application to shut down unused modules in the mcu, thereby saving power. the avr provides various sleep modes allowing the user to tailor the power consumption to the application?s requirements. 7.1 sleep modes figure 6-1 on page 17 presents the different clock systems and their distribution in attiny4/5/9/10. the figure is helpful in selecting an appropriate sleep mode. table 7-1 shows the different sleep modes and their wake up sources. note: 1. the adc is available in attiny5/10, only 2. for int0, only level interrupt. to enter any of the four sleep modes, the se bits in smcr must be written to logic one and a sleep instruction must be execut ed. the sm2:0 bits in the smcr register select which sleep mode (idle, adc noise reduction, standby or power-down) will be ac tivated by the sleep instruction. see table 7-2 for a summary. if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles in addition to the st art-up time, executes the interrupt routine, and resumes execution from the instruction followi ng sleep. the contents of the register file and sram are unaltered when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. note that if a level triggered interrupt is used for wake-up the changed level must be held for some time to wake up the mcu (and for the mc u to enter the interrupt service routine). see ?external interrupts? on page 36 for details. 7.1.1 idle mode when bits sm2:0 are written to 000, the sleep instruction ma kes the mcu enter idle mode, stopping the cpu but allowing the analog compar ator, timer/counter, wa tchdog, and the inter- rupt system to continue operating. this sleep mode basically halts clk cpu and clk nvm , while allowing the other clocks to run. idle mode enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow. if wake-up from the analog comparator interrupt is not required, the table 7-1. active clock domains and wake-up sources in different sleep modes sleep mode active clock domains oscillators wake-up sources clk cpu clk nvm clk io clk adc (1) main clock source enabled int0 and pin change adc (1) other i/o watchdog interrupt vlm interrupt idle xx x xxxxx adc noise reduction x x x (2) xxx standby x x (2) x power-down x (2) x
24 8127c?avr?10/09 attiny4/5/9/10 analog comparator can be powered down by setting the acd bit in ?acsr ? analog comparator control and status register? on page 81 . this will reduce power cons umption in idle mode. if the adc is enabled (attiny5/10, only), a conversion starts automatically when this mode is entered. 7.1.2 adc noise reduction mode when bits sm2:0 are written to 001, the sleep instruction makes the mcu enter adc noise reduction mode, stopping the cpu but allowing the adc, the external interrupts, and the watch- dog to continue operating (if enabl ed). this sleep mode halts clk i/o , clk cpu , and clk nvm , while allowing the other clocks to run. this mode improves the noise environment for the adc, enabling higher resolution measure- ments. if the adc is enabled, a conversion st arts automatically when this mode is entered. this mode is available in all devices, altho ugh only attiny5/10 are equipped with an adc. 7.1.3 power-down mode when bits sm2:0 are written to 010, the slee p instruction makes the mcu enter power-down mode. in this mode, the oscilla tor is stopped, while the external interr upts, and the watchdog continue operating (if enabled). only a watchdog reset, an external level interrupt on int0, or a pin change interrupt can wake up the mcu. this sleep mode halts all generated clocks, allowing operation of asynchronous modules only. 7.1.4 standby mode when bits sm2:0 are written to 100, the sl eep instruction makes the mcu enter standby mode. this mode is identical to power-down with the exception that the oscillator is kept run- ning. this reduces wake-up time, because the oscillator is already running and doesn't need to be started up. 7.2 power reduction register the power reduction register (prr), see ?prr ? power reduction register? on page 26 , pro- vides a method to reduce power consumption by stopping the clock to individual peripherals. when the clock for a peripheral is stopped then: ? the current state of the peripheral is frozen. ? the associated registers can not be read or written. ? resources used by the peri pheral will remain occupied. the peripheral should in most cases be disabled before stopping the clock. clearing the prr bit wakes up the peripheral and puts it in the same state as before shutdown. peripheral shutdown can be used in idle mode an d active mode to significantly reduce the over- all power consumption. see ?supply current of i/o modules? on page 122 for examples. in all other sleep modes, the clock is already stopped. 7.3 minimizing power consumption there are several issues to consider when tryi ng to minimize the power consumption in an avr core controlled system. in general, sleep modes should be used as much as possible, and the sleep mode should be selected so that as few as possible of the device? s functions are operat- ing. all functions not needed shoul d be disabled. in particular, the following modules may need special consideration when trying to achi eve the lowest possible power consumption.
25 8127c?avr?10/09 attiny4/5/9/10 7.3.1 analog comparator when entering idle mode, the analog comparator should be disabled if not used. in the power- down mode, the analog comparator is automatically disabled. see ?analog comparator? on page 81 for further details. 7.3.2 analog to digital converter if enabled, the adc will be enabled in all sleep modes. to save power, the adc should be dis- abled before entering any sleep mode. when th e adc is turned off and on again, the next conversion will be an ex tended conversion. see ?analog to digital converter? on page 83 for details on adc operation. the adc is available in attiny5/10, only. 7.3.3 watchdog timer if the watchdog timer is not needed in the application, this module should be turned off. if the watchdog timer is enabled, it will be enabled in all sleep modes, and hence, always consume power. in the deeper slee p modes, this will contribute signific antly to the total current consump- tion. refer to ?watchdog timer? on page 30 for details on how to configure the watchdog timer. 7.3.4 port pins when entering a sleep mode, all port pins should be configured to use minimum power. the most important thing is then to ensure that no pins drive resistive loads. in sleep modes where the i/o clock (clk i/o ) is stopped, the input buffers of the device will be disabled. this ensures that no power is consumed by the input logic when not needed. in some cases, the input logic is needed for detecting wake-up co nditions, and it will then be enabled. refer to the section ?digital input enable and sleep modes? on page 44 for details on which pins are enabled. if the input buffer is enabled and the input signal is left fl oating or has an analog signal level close to v cc /2, the input buffer will use excessive power. for analog input pins, the digital input buffer should be disabled at all times. an analog signal level close to v cc /2 on an input pin can cause significant current even in active mode. digital input buffers can be disabled by writing to the digital input disable regi ster (didr0). refer to ?didr0 ? digital input disable register 0? on page 82 for details. 7.4 register description 7.4.1 smcr ? sleep mode control register the smcr control register contains control bits for power management. ? bits 7:4 ? res: reserved bits these bits are reserved and will always read zero. bit 76543210 0x3a ? ? ? ? sm2 sm1 sm0 se smcr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
26 8127c?avr?10/09 attiny4/5/9/10 ? bits 3:1 ? sm2..sm0: sleep mode select bits 2..0 these bits select between availa ble sleep modes, as shown in table 7-2 . note: 1. this mode is available in all devices, although only attiny5/10 are equipped with an adc ? bit 0 ? se: sleep enable the se bit must be written to logic one to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmer?s purpose, it is recommended to wr ite the sleep enable (se) bit to one just before the execution of the sleep instruction and to clear it immediately af ter waking up. 7.4.2 prr ? power reduction register ? bits 7:2 ? res: reserved bits these bits are reserved and will always read zero. ? bit 1 ? pradc: power reduction adc writing a logic one to this bit shuts down the a dc. the adc must be disabled before shut down. the analog comparator cannot use the adc input mux when the adc is shut down. the adc is available in attiny5/10, only. ? bit 0 ? prtim0: power reduction timer/counter0 writing a logic one to this bit shuts down the timer/counter0 module. when the timer/counter0 is enabled, operation will cont inue like before the shutdown. table 7-2. sleep mode select sm2 sm1 sm0 sleep mode 000idle 0 0 1 adc noise reduction (1) 010power-down 011reserved 100standby 101reserved 110reserved 111reserved bit 7 6 5 4 3 2 1 0 0x35 ? ? ? ? ? ? pradc prtim0 prr read/write r r r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
27 8127c?avr?10/09 attiny4/5/9/10 8. system control and reset 8.1 resetting the avr during reset, all i/o registers are set to their in itial values, and the program starts execution from the reset vector. the instruction placed at the re set vector must be a rjmp ? relative jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 8-1 shows the reset logic. electrical pa rameters of the re set circuitry are defined in section ?system and reset characteristics? on page 119 . figure 8-1. reset logic the i/o ports of the avr are immediately reset to their initial state when a reset source goes active. this does not require any clock source to be running. after all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. this allows the power to reach a stable level before normal operation starts. the start up sequence is described in ?starting from reset? on page 20 . 8.2 reset sources the attiny4/5/9/10 have three sources of reset: ? power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ) ? external reset. the mcu is reset when a low level is pr esent on the reset pin for longer than the minimum pulse length ? watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled reset flag register (rstflr) delay counters ck timeout wdrf extrf porf vlmrf data bus clock generator spike filter pull-up resistor watchdog oscillator power-on reset circuit
28 8127c?avr?10/09 attiny4/5/9/10 8.2.1 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detection level is defined in section ?system and reset characteristics? on page 119 . the por is activated whenever v cc is below the detection level. the por ci rcuit can be used to trigger the start-up reset, as well as to detect a failure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reaching the power-on reset threshold voltage invokes the delay counter, which determines how long the device is kept in reset after v cc rise. the reset signal is activated again, without any delay, when v cc decreases below the detection level. figure 8-2. mcu start-up, reset tied to v cc figure 8-3. mcu start-up, reset extended externally 8.2.2 v cc level monitoring attiny4/5/9/10 have a v cc level monitoring (vlm) circuit that compares the voltage level at the v cc pin against fixed trigger levels. the trigger levels are set with vlm2:0 bits, see ?vlmcsr ? vcc level monitoring control an d status register? on page 33 . the vlm circuit provides a status flag, vl mf, that indicates if voltage on the v cc pin is below the selected trigger level. the flag can be read from vlmcsr, but it is also possible to have an interrupt generated when the vlmf st atus flag is set. this interrupt is enabled by the vlmie bit in the vlmcsr register. the flag can be cleared by changing the trigger level or by writing it to zero. the flag is automatically cleared when the voltage at v cc rises back above the selected trigger level. v reset time-out internal reset t tout v pot v rst cc reset time-out internal reset t tout v pot v rst v cc
29 8127c?avr?10/09 attiny4/5/9/10 the vlm can also be used to im prove reset characteristics at falling supply. without vlm, the power-on reset (por) does not activate bef ore supply voltage has dropped to a level where the mcu is not necessarily functional any more. wi th vlm, it is possible to generate a reset at supply voltages where the mcu is still functional. when active, the vlm circuit consum es some power, as illustrated in figure 17-48 on page 146 . to save power the vlm circuit can be turned off completely, or it can be switched on and off at regular intervals. however, dete ction takes some time and it is therefore recommended to leave the circuitry on long enough for signals to settle. see ?vcc level monitor? on page 119 . when vlm is active and voltage at v cc is above the selected trigger level operation will be as normal and the vlm can be shut down for a short period of time. if voltage at v cc drops below the selected threshold the vlm will ei ther flag an interrupt or gene rate a reset, de pending on the configuration. when the vlm has been configur ed to generate a reset at lo w supply voltage it will keep the device in reset as long as v cc is below the reset level. see table 8-4 on page 34 for reset level details. if supply voltage rises above the reset level the condition is removed and the mcu will come out of reset, and initiate the power-up start-up sequence. if supply voltage drops enough to trigger the por then porf is set after supply voltage has been restored. 8.2.3 external reset an external reset is generated by a low level on the reset pin if enabled. reset pulses longer than the minimum pulse width (see section ?system and reset characteristics? on page 119 ) will generate a reset, even if th e clock is not running. shorter pulses are not g uaranteed to gen- erate a reset. when the applied signal reaches the reset threshold voltage ? v rst ? on its positive edge, the delay counter starts the mcu after the time-out period ? t tout ? has expired. figure 8-4. external reset during operation 8.2.4 watchdog reset when the watchdog times out, it will generate a short reset pulse of one ck cycle duration. on the falling edge of this pulse, the delay time r starts counting the time-out period t tout . see page 30 for details on operation of the watchdog timer and table 16-4 on page 119 for details on reset time-out. cc
30 8127c?avr?10/09 attiny4/5/9/10 figure 8-5. watchdog reset du ring operation 8.3 watchdog timer the watchdog timer is clocked from an on-c hip oscillator, which runs at 128 khz. see figure 8- 6 . by controlling the watchdog ti mer prescaler, the watchdog rese t interval can be adjusted as shown in table 8-2 on page 32 . the wdr ? watchdog reset ? instruction resets the watchdog timer. the watchdog timer is also reset when it is disabled and when a device reset occurs. ten different clock cycle periods can be selected to determine the reset period. if the reset period expires without another watchdog reset, t he attiny4/5/9/10 resets and executes from the reset vector. for timing details on the watchdog reset, refer to table 8-3 on page 33 . figure 8-6. watchdog timer the wathdog timer can also be configured to g enerate an interrupt instead of a reset. this can be very helpful when using the watchdog to wake-up from power-down. to prevent unintentional disabling of the watchdog or unintentional change of time-out period, two different safety levels are sele cted by the fuse wd ton as shown in table 8-1 on page 31 . see ?procedure for changing the watchdog timer configuration? on page 31 for details. ck cc osc/2k osc/4k osc/8k osc/16k osc/32k osc/64k osc/128k osc/256k osc/512k osc/1024k mcu reset watchdog prescaler 128 khz oscillator watchdog reset wdp0 wdp1 wdp2 wdp3 wde mux
31 8127c?avr?10/09 attiny4/5/9/10 8.3.1 procedure for changing the watchdog timer configuration the sequence for changing configuration differs between the two safety levels, as follows: 8.3.1.1 safety level 1 in this mode, the watchdog time r is initially disabled, but can be enabled by writing the wde bit to one without any restriction. a special sequence is needed when disabling an enabled watch- dog timer. to disable an enabled watchdog timer, the following procedure must be followed: 1. write the signature for change enable of protected i/o registers to register ccp 2. within four instruction cycles, in t he same operation, write wde and wdp bits 8.3.1.2 safety level 2 in this mode, the watchdog time r is always enabled, and the wde bit will always read as one. a protected change is needed when changing the watchdog time-out period. to change the watchdog time-out, the following procedure must be followed: 1. write the signature for change enable of protected i/o registers to register ccp 2. within four instruction cycles, write the wd p bit. the value written to wde is irrelevant 8.3.2 code examples the following code example shows how to turn off the wdt. the example assumes that inter- rupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during execution of these functions. note: see ?code examples? on page 5 . table 8-1. wdt configuration as a function of the fuse settings of wdton wdton safety level wdt initial state how to disable the wdt how to change time-out unprogrammed 1 disabled protected change sequence no limitations programmed 2 enabled always enabled protected change sequence assembly code example wdt_off: wdr ; clear wdrf in rstflr in r16, rstflr andi r16, ~(1< 32 8127c?avr?10/09 attiny4/5/9/10 8.4 register description 8.4.1 wdtcsr ? watchdog timer control and status register ? bit 7 ? wdif: watchdog timer interrupt flag this bit is set when a time-out occurs in the watchdog timer and the watchdog timer is config- ured for interrupt. wdif is cleared by hardw are when executing the corresponding interrupt handling vector. alternatively, wd if is cleared by writing a logic one to the flag. when the wdie is set, the watchdog time-o ut interrupt is requested. ? bit 6 ? wdie: watchdog timer interrupt enable when this bit is written to one, the watchdog interrupt request is enabled. if wde is cleared in combination with this setting, the watchdog time r is in interrupt mode, and the corresponding interrupt is requested if time-out in the watchdog timer occurs. if wde is set, the watchdog timer is in interrupt and system reset mode. the first time-out in the watchdog timer will set wdif. executing t he corresponding interrup t vector will clear wdie and wdif automatically by hardw are (the watchdog goes to system reset mode). this is use- ful for keeping the watchdog timer security while using the interrupt. to stay in interrupt and system reset mode, wdie must be set after each interrupt. this should however not be done within the interrupt service routine itself, as th is might compromise the safety-function of the watchdog system reset mode. if the interrupt is not executed before the next time-out, a sys- tem reset will be applied. note: 1. wdton configuration bit set to ?0? means programmed and ?1? means unprogrammed. ? bit 4 ? res: reserved bit this bit is reserved and will always read zero. ? bit 3 ? wde: watchdog system reset enable wde is overridden by wdrf in mcusr. this m eans that wde is always set when wdrf is set. to clear wde, wdrf must be cleared first. this feature ensures multiple resets during con- ditions causing failure, and a safe start-up after the failure. bit 76543210 0x31 wdif wdie wdp3 ? wde wdp2 wdp1 wdp0 wdtcsr read/write r/w r/w r/w r r/w r/w r/w r/w initial value 0 0 0 0 x 0 0 0 table 8-2. watchdog timer configuration wdton (1) wde wdie mode action on time-out 1 0 0 stopped none 1 0 1 interrupt mode interrupt 1 1 0 system reset mode reset 111 interrupt and system reset mode interrupt, then go to system reset mode 0 x x system reset mode reset
33 8127c?avr?10/09 attiny4/5/9/10 ? bits 5, 2:0 ? wdp3..0: watchdog timer prescaler 3, 2, 1 and 0 the wdp3..0 bits determine the watchdog timer prescaling when the watchdog timer is run- ning. the different prescaling values and their corresponding ti me-out periods are shown in table 8-3 on page 33 . 8.4.2 vlmcsr ? v cc level monitoring control and status register ? bit 7 ? vlmf: vlm flag this bit is set by the vlm circuit to indicate th at a voltage level condition has been triggered (see table 8-4 ). the bit is cleared when the trigger level se lection is set to ?disabled?, or when volt- age at v cc rises above the selected trigger level. ? bit 6 ? vlmie: vlm interrupt enable when this bit is set the vlm interrupt is enabl ed. a vlm interrupt is generated every time the vlmf flag is set. ? bits 5:3 ? res: reserved bits these bits are reserved. for ensuring compatibilit y with future devices, these bits must be writ- ten to zero, when the register is written. table 8-3. watchdog timer prescale select wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v 0000 2k (2048) cycles 16 ms 0001 4k (4096) cycles 32 ms 0010 8k (8192) cycles 64 ms 0011 16k (16384) cycles 0.125 s 0100 32k (32768) cycles 0.25 s 0101 64k (65536) cycles 0.5 s 0110 128k (131072) cycles 1.0 s 0111 256k (262144) cycles 2.0 s 1000 512k (524288) cycles 4.0 s 1001 1024k (1048576) cycles 8.0 s 1010 reserved 1011 1100 1101 1110 1111 bit 76543210 0x34 vlmf vlmie ? ? ? vlm2 vlm1 vlm0 vlmcsr read/write r r/w r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
34 8127c?avr?10/09 attiny4/5/9/10 ? bits 2:0 ? vlm2:0: trigger level of voltage level monitor these bits set the trigger level for the voltage level monitor, as described in table 8-4 below. for vlm voltage levels, see tbd, tbd and tbd. 8.4.3 rstflr ? reset flag register the reset flag register provides informatio n on which reset source caused an mcu reset. ? bits 7:4, 2? res: reserved bits these bits are reserved bits in atti ny4/5/9/10 and will always read as zero. ? bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog re set occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a rese t condition, the user should read and then reset the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. table 8-4. setting the trigger level of voltage level monitor. vlm2:0 label description 000 vlm0 voltage level monitor disabled 001 vlm1l triggering generates a regu lar power-on reset (por). the vlm flag is not set 010 vlm1h 011 vlm2 triggering sets the vlm flag (vlmf) and generates a vlm interrupt, if enabled 100 vlm3 101 not allowed 110 111 bit 76543210 0x3b ? ? ? ? wdrf ? extrf porf rstflr read/write r r r r r/w r r/w r/w initial value 0 0 0 0 x 0 x x
35 8127c?avr?10/09 attiny4/5/9/10 9. interrupts this section describes the specifics of the interrupt hand ling in attiny4/5/9/10. for a general explanation of the avr interrupt handling, see ?reset and interrupt handling? on page 10 . 9.1 interrupt vectors interrupt vectors of attiny4/5/9/10 are described in table 9-1 below. note: 1. the adc is available in attiny5/10, only. in case the program never enab les an interrupt source, the interrupt vectors will not be used and, consequently, regular program code can be placed at these locations. the most typical and general setup for interrupt vector addresses in atti ny4/5/9/10 is shown in the program example below. address labels code comments 0x0000 rjmp reset ; reset handler 0x0001 rjmp int0 ; irq0 handler 0x0002 rjmp pcint0 ; pcint0 handler 0x0003 rjmp tim0_capt ; timer0 capture handler 0x0004 rjmp tim0_ovf ; timer0 overflow handler 0x0005 rjmp tim0_compa ; timer0 compare a handler 0x0006 rjmp tim0_compb ; timer0 compare b handler 0x0007 rjmp ana_comp ; analog comparator handler 0x0008 rjmp wdt ; watchdog interrupt handler 0x0009 rjmp vlm ; voltage level monitor handler 0x000a rjmp adc ; adc conversion handler table 9-1. reset and interrupt vectors vector no. program address label interrupt source 1 0x0000 reset external pin, power-on reset, vlm reset, watchdog reset 2 0x0001 int0 external interrupt request 0 3 0x0002 pcint0 pin change interrupt request 0 4 0x0003 tim0_capt timer/counter0 input capture 5 0x0004 tim0_ovf timer/counter0 overflow 6 0x0005 tim0_compa timer/counter0 compare match a 7 0x0006 tim0_compb timer/counter0 compare match b 8 0x0007 ana_comp analog comparator 9 0x0008 wdt watchdog time-out 10 0x0009 vlm v cc voltage level monitor 11 0x000a adc adc conversion complete (1)
36 8127c?avr?10/09 attiny4/5/9/10 0x000b reset: ldi r16, high(ramend); main program start 0x000c out sph,r16 ; set stack pointer 0x000d ldi r16, low(ramend) ; to top of ram 0x000e out spl,r16 0x000f sei ; enable interrupts 0x0010 ... ... 9.2 external interrupts external interrupts are triggered by the int0 pin or any of the pcint3..0 pins. observe that, if enabled, the interrupts will trigger even if the int0 or pcint3 ..0 pins are config ured as outputs. this feature provides a way of generating a softw are interrupt. pin change 0 interrupts pci0 will trigger if any enabled pcint3..0 pin toggles. the pcmsk register controls which pins contrib- ute to the pin change interrupts. pin change interrupts on pcint3..0 are detected asynchronously, which means that these interrupts can be used for waking the part also from sleep modes other than idle mode. the int0 interrupt can be triggered by a falling or rising edge or a low level. this is set up as shown in ?eicra ? external interrupt control register a? on page 37 . when the int0 interrupt is enabled and confi gured as level triggered, th e interrupt will trigger as long as the pin is held low. note that recognition of falling or rising edge interrupts on int0 requires the presen ce of an i/o clock, as described in ?clock system? on page 17 . 9.2.1 low level interrupt a low level interrupt on int0 is detected asyn chronously. this means that the interrupt source can be used for waking the part also from sleep modes other than idle (the i/o clock is halted in all sleep modes except idle). note that if a level triggered interrupt is used for wake-up from power-down, the required level must be held long enough for the mcu to complete t he wake-up to trigger the level interrupt. if the level disappears before the end of the start-up ti me, the mcu will still wake up, but no inter- rupt will be generated. th e start-up time is defined as described in ?clock system? on page 17 . if the low level on the interrupt pin is removed before the device has woken up then program execution will not be diverted to the interrupt service ro utine but continue from the instruction fol- lowing the sleep command. 9.2.2 pin change interrupt timing a timing example of a pin change interrupt is shown in figure 9-1 .
37 8127c?avr?10/09 attiny4/5/9/10 figure 9-1. timing of pin change interrupts 9.3 register description 9.3.1 eicra ? external inte rrupt control register a the external interrupt control register a cont ains control bits for interrupt sense control. ? bits 7:2 ? res: reserved bits these bits are reserved and will always read zero. ? bits 1:0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the exte rnal pin int0 if the sr eg i-flag and the corre- sponding interrupt mask are set. the level and edges on the external int0 pin that activate the interrupt are defined in table 9-2 . the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is clk pcint(0) pin_lat pin_sync pcint_in_(0) pcint_syn pcint_setflag pcif pcint(0) pin_sync pcint_syn pin_lat d q le pcint_setflag pcif clk clk pcint(0) in pcmsk(x) pcint_in_(0) 0 x bit 76543210 0x15 ? ? ? ? ? ? isc01 isc00 eicra read/write rrrrrrr/wr/w initial value 0 0 0 0 0 0 0 0
38 8127c?avr?10/09 attiny4/5/9/10 selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. 9.3.2 eimsk ? external interrupt mask register ? bits 7:1 ? res: reserved bits these bits are reserved and will always read zero. ? bit 0 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in th e status register (sreg) is set (one), the exter- nal pin interrupt is enabled. the interrupt sense control bits (isc01 and isc00) in the external interrupt control register a (eic ra) define whether the external interrupt is activated on rising and/or falling edge of the int0 pin or level sens ed. activity on the pin will cause an interrupt request even if int0 is configured as an output. the corresponding interrupt of external interrupt request 0 is executed from the int0 interrupt vector. 9.3.3 eifr ? external interrupt flag register ? bits 7:1 ? res: reserved bits these bits are reserved and will always read zero. ? bit 0 ? intf0: external interrupt flag 0 when an edge or logic change on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in eimsk are set (o ne), the mcu will jump to the cor- responding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. this flag is constantly zero when in t0 is configured as a level interrupt. table 9-2. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request. bit 76543210 0x13 ? ? ? ? ? ? ?intoeimsk read/write rrrrrrrr/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x14 ? ? ? ? ? ? ? intf0 eifr read/write rrrrrrrr/w initial value 0 0 0 0 0 0 0 0
39 8127c?avr?10/09 attiny4/5/9/10 9.3.4 pcicr ? pin change in terrupt control register ? bits 7:1 ? res: reserved bits these bits are reserved and will always read zero. ? bit 0 ? pcie0: pin change interrupt enable 0 when the pcie0 bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt 0 is enabled. any change on any enabl ed pcint3..0 pin will cause an interrupt. the corresponding interrupt of pin change interrupt request is executed from the pci0 inter- rupt vector. pcint3..0 pins are enabled individually by the pcmsk register. 9.3.5 pcifr ? pin change interrupt flag register ? bits 7:1 ? res: reserved bits these bits are reserved and will always read zero. ? bit 0 ? pcif0: pin change interrupt flag 0 when a logic change on any pcint3..0 pin triggers an interrupt request, pcif0 becomes set (one). if the i-bit in sreg and the pcie0 bit in pcicr are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt rout ine is executed. alter- natively, the flag can be cleared by writing a logical one to it. 9.3.6 pcmsk ? pin change mask register ? bits 7:4 ? res: reserved bits these bits are reserved and will always read zero. ? bits 3:0 ? pcint3..0: pin change enable mask 3..0 each pcint3..0 bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint3..0 is set and the pcie0 bit in pci cr is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint3 ..0 is cleared, pin change interrupt on the corresponding i/o pin is disabled. bit 76543210 0x12 ? ? ? ? ? ? ?pcie0pcicr read/write rrrrrrrr/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x11 ? ? ? ? ? ? ? pcif0 pcifr read/write rrrrrrrr/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x10 ? ? ? ? pcint3 pcint2 pcint1 pcint0 pcmsk read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
40 8127c?avr?10/09 attiny4/5/9/10 10. i/o ports 10.1 overview all avr ports have true read-modi fy-write functionality when used as general digital i/o ports. this means that the dire ction of one port pin can be chan ged without unintentionally changing the direction of any other pin with the sbi and cbi instructions. the same applies when chang- ing drive value (if configured as output) or enab ling/disabling of pull-up resistors. each output buffer has symmetrical drive char acteristics with both high sink and source capability. the pin driver is strong enough to drive led displays directly. all port pins have individually selectable pull-up resistors with a supply-voltage invariant re sistance. all i/o pins have protection diodes to both v cc and ground as indicated in figure 10-1 on page 40 . see ?electrical characteristics? on page 116 for a complete list of parameters. figure 10-1. i/o pin equivalent schematic all registers and bit references in this section are written in general form. a lower case ?x? repre- sents the numbering letter for the port, and a lower case ?n? represents the bit number. however, when using the register or bit defines in a progr am, the precise form must be used. for example, portb3 for bit no. 3 in port b, here document ed generally as portxn. the physical i/o regis- ters and bit locations are listed in ?register description? on page 50 . four i/o memory address locations are allocated for each port, one each for the data register ? portx, data direction register ? ddrx, pull -up enable register ? puex, and the port input pins ? pinx. the port input pins i/o location is read only, while the data register, the data direction register, and the pull-up enable regist er are read/write. howe ver, writing a logic one to a bit in the pinx register, will result in a togg le in the corresponding bit in the data register. using the i/o port as general digital i/o is described in ?ports as general digital i/o? on page 41 . most port pins are multiplexed with alternate functions for the peripheral features on the device. how each alternate function interferes with the port pin is described in ?alternate port functions? on page 45 . refer to the individual module sectio ns for a full description of the alter- nate functions. note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital i/o. c pin logic r pu s ee figure "general digital i/o" for details pxn
41 8127c?avr?10/09 attiny4/5/9/10 10.2 ports as general digital i/o the ports are bi-directional i/o ports with optional internal pull-ups. figure 10-2 shows a func- tional description of one i/o-port pin, here generically called pxn. figure 10-2. general digital i/o (1) note: 1. wex, wrx, wpx, wdx, rex, rrx, rpx, and rd x are common to all pins within the same port. clk i/o , and sleep are common to all ports. 10.2.1 configuring the pin each port pin consists of four register bits : ddxn, portxn, puexn, and pinxn. as shown in ?register description? on page 50 , the ddxn bits are accessed at the ddrx i/o address, the portxn bits at the portx i/o address, the puex n bits at the puex i/o address, and the pinxn bits at the pinx i/o address. clk rpx rrx rdx wdx wex synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin clk i/o : i/o clock rdx: read ddrx wex: write puex rex: read puex d l q q rex reset reset q q d q qd clr portxn q qd clr ddxn pinxn data bus sleep sleep: sleep control pxn i/o wpx reset q qd clr puexn 0 1 wrx wpx: write pinx register
42 8127c?avr?10/09 attiny4/5/9/10 the ddxn bit in the ddrx register selects the direct ion of this pin. if ddxn is written logic one, pxn is configured as an output pin. if ddxn is written logic ze ro, pxn is configured as an input pin. if portxn is written logic one when the pin is configured as an ou tput pin, the port pin is driven high (one). if portxn is writte n logic zero when the pin is config ured as an output pin, the port pin is driven low (zero). the pull-up resistor is activated, if the puexn is written logic o ne. to switch the pull-up resistor off, puexn has to be written logic zero. table 10-1 summarizes the control signals for the pin value. port pins are tri-stated when a reset cond ition becomes active, even when no clocks are running. 10.2.2 toggling the pin writing a logic one to pinxn toggles the value of portxn, independent on the value of ddrxn. note that the sbi instruction can be us ed to toggle one single bit in a port. 10.2.3 break-before-make switching in break-before-make mode, switching the ddrxn bit from input to output introduces an imme- diate tri-state period lasting one s ystem clock cycle, as indicated in figure 10-3 . for example, if the system clock is 4 mhz and the ddrxn is wri tten to make an output, an immediate tri-state period of 250 ns is introduced before the value of portxn is seen on the port pin. to avoid glitches it is re commended that the maximum ddrxn toggle frequency is two system clock cycles. the break-be fore-make mode applies to the entire port and it is activated by the bbmx bit. for more details, see ?portcr ? port control register? on page 50 . when switching the ddrxn bit from output to input no immediate tr i-state period is introduced. table 10-1. port pin configurations ddxn portxn puexn i/o pull-up comment 0x 0 input no tri-state (hi-z) 0x 1 input yes sources current if pulled low externally 10 0 output no output low (sink) 10 1 output yes not recommended. output low (sink) and internal pull-up active. sources current through the internal pull-up resistor and consumes power constantly 11 0 output no output high (source) 11 1 output yes output high (source) and internal pull-up active
43 8127c?avr?10/09 attiny4/5/9/10 figure 10-3. switching between input and output in break-before-make-mode 10.2.4 reading the pin value independent of the setting of data direction bit ddxn, the port pin can be read through the pinxn register bit. as shown in figure 10-2 on page 41 , the pinxn register bit and the preced- ing latch constitute a synchronizer. this is ne eded to avoid metastability if the physical pin changes value near the edge of the internal clock, but it also introduces a delay. figure 10-4 shows a timing diagram of the synchronization when reading an externally applied pin value. the maximum and minimum propagation delays are denoted t pd,max and t pd,min respectively. figure 10-4. synchronization when reading an externally applied pin value consider the clock period starting shortly after the first falling edge of the system cl ock. the latch is closed when the clock is low, and goes transpa rent when the clock is high, as indicated by the shaded region of the ?sync latch? signal. the signal value is latched when the system clock goes low. it is clocked into the pinxn register at the succeeding positive clock edge. as indi- cated by the two arrows tpd,max and tpd,min, a single signal tr ansition on the pin will be delayed between ? and 1? system clock period depending upon the time of assertion. out ddrx, r16 nop 0x02 0x01 system clk instructions ddrx intermediate tri-state cycle out ddrx, r17 0x55 portx 0x01 intermediate tri-state cycle px0 px1 tri-state tri-state tri-state 0x01 r17 0x02 r16 xxx in r17, pinx 0x00 0xff instructions sync latch pinxn r17 xxx system clk t pd, m a x t pd, min
44 8127c?avr?10/09 attiny4/5/9/10 when reading back a software assigned pin value, a nop instruction must be inserted as indi- cated in figure 10-5 on page 44 . the out instruction sets the ?sync latch? signal at the positive edge of the clock. in this case, the delay tpd through the synchronizer is one system clock period. figure 10-5. synchronization when reading a software assigned pin value 10.2.5 digital input enable and sleep modes as shown in figure 10-2 on page 41 , the digital input signal can be clamped to ground at the input of the schmitt-trigger. th e signal denoted sleep in the fi gure, is set by the mcu sleep controller in power-down and standby modes to avoid high power consumption if some input signals are left floating, or have an analog signal level close to v cc /2. sleep is overridden for port pins enabled as ex ternal interrupt pins. if the external interrupt request is not e nabled, sleep is active also for these pi ns. sleep is also over ridden by various other alternate functions as described in ?alternate port functions? on page 45 . if a logic high level (?one?) is present on an asyn chronous external interrupt pin configured as ?interrupt on rising edge, falling edge, or any logic change on pin? while the external interrupt is not enabled, the corresponding external interrupt flag will be set when resuming from the above mentioned sleep mode, as the clamping in these sleep mode produces the requested logic change. 10.2.6 unconnected pins if some pins are unused, it is recommended to ensure that these pins have a defined level. even though most of the digital inputs are disabled in the deep sleep modes as described above, float- ing inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (reset, active mode and idle mode). the simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. in this case, the pull-up will be disabled during reset. if low po wer consumption during reset is important, it is recommended to use an external pull-up or pulldown. connecting unused pins directly to v cc or gnd is not recommended, since this ma y cause excessive curr ents if the pin is accidentally configured as an output. out portx, r16 nop in r17, pinx 0xff 0x00 0xff system clk r16 instructions sync latch pinxn r17 t pd
45 8127c?avr?10/09 attiny4/5/9/10 10.2.7 program example the following code example shows how to set port b pin 0 high, pin 1 low, and define the port pins from 2 to 3 as input with a pull-up assigned to port pin 2. the resulting pin values are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pins. note: see ?code examples? on page 5 . 10.3 alternate port functions most port pins have alternat e functions in addition to being general digital i/os. in figure 10-6 below is shown how the port pin c ontrol signals from the simplified figure 10-2 on page 41 can be overridden by alternate functions. assembly code example ... ; define pull-ups and set outputs high ; define directions for port pins ldi r16,(1< 46 8127c?avr?10/09 attiny4/5/9/10 figure 10-6. alternate port functions (1) note: 1. wex, wrx, wpx, wdx, rex, rrx, rpx, and rd x are common to all pins within the same port. clk i/o , and sleep are common to all ports. all other signals are unique for each pin. the illustration in the figure above serves as a generic description applicable to all port pins in the avr microcontroller family. some overriding signals may not be present in all port pins. clk rpx rrx wrx rdx wdx synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin rex: read puex wex: write puex clk i/o : i/o clock rdx: read ddrx d l q q set clr 0 1 0 1 0 1 dixn aioxn dieoexn pvovxn pvoexn ddovxn ddoexn puoexn puovxn puoexn: pxn pull-up override enable puovxn: pxn pull-up override value ddoexn: pxn data direction override enable ddovxn: pxn data direction override value pvoexn: pxn port value override enable pvovxn: pxn port value override value dixn: digital input pin n on portx aioxn: analog input/output pin n on portx reset reset q qd clr q qd clr q q d clr pinxn portxn ddxn data bus 0 1 dieovxn sleep dieoexn: pxn digital input-enable override enable dieovxn: pxn digital input-enable override value sleep: sleep control pxn i/o 0 1 ptoexn ptoexn: pxn, port toggle override enable wpx: write pinx wpx wex rex reset q qd clr puexn
47 8127c?avr?10/09 attiny4/5/9/10 table 10-2 on page 47 summarizes the function of the overriding signals. the pin and port indexes from figure 10-6 on page 46 are not shown in the succeeding tables. the overriding signals are generated internally in the modules having the alternate function. the following subsections shortly describe the alte rnate functions for each port, and relate the overriding signals to the alternate function. refer to the alternate function description for further details. table 10-2. generic description of overriding signals for alternate functions signal name full name description puoe pull-up override enable if this signal is set, the pull-up enable is controlled by the puov signal. if this signal is cleared, the pull-up is enabled when puexn = 0b1. puov pull-up override value if puoe is set, the pull-up is enabled/disabled when puov is set/cleared, regardless of the setting of the puexn register bit. ddoe data direction override enable if this signal is set, the output driver enable is controlled by the ddov signal. if this signal is cl eared, the output driver is enabled by the ddxn register bit. ddov data direction override value if ddoe is set, the output dr iver is enabled/disabled when ddov is set/cleared, regardless of the setting of the ddxn register bit. pvoe port value override enable if this signal is set and the outp ut driver is enabled, the port value is controlled by the pvov si gnal. if pvoe is cleared, and the output driver is enabled, the port value is controlled by the portxn register bit. pvov port value override value if pvoe is set, the port value is set to pvov, regardless of the setting of the portxn register bit. ptoe port toggle override enable if ptoe is set, the portxn register bit is inverted. dieoe digital input enable override enable if this bit is set, the digital input enable is controlled by the dieov signal. if this signal is cleared, the digital input enable is determined by mcu state (normal mode, sleep mode). dieov digital input enable override value if dieoe is set, the digital input is enabled/disabled when dieov is set/cleared, regardl ess of the mcu state (normal mode, sleep mode). di digital input this is the digital input to alte rnate functions. in the figure, the signal is connected to the out put of the schmitt-trigger but before the synchronizer. unless the digital input is used as a clock source, the module with the alternate function will use its own synchronizer. aio analog input/output this is the analog input/output to /from alternate functions. the signal is connected directly to the pad, and can be used bi- directionally.
48 8127c?avr?10/09 attiny4/5/9/10 10.3.1 alternate functions of port b the port b pins with alte rnate function are shown in table 10-3 on page 48 . ? port b, bit 0 ? adc0/ain0/oc0a/pcint0/tpidata ? adc0: analog to digital converter, channel 0 (attiny5/10, only) ? ain0: analog comparator positive input. configure the port pin as input with the internal pull- up switched off to avoid the digital port functi on from interfering with the function of the analog comparator. ? oc0a, output compare match output: the pb0 pi n can serve as an external output for the timer/counter0 compare match a. the pin has to be configured as an output (ddb0 set (one)) to serve this function. this is also the output pin for the pwm mode timer function. ? pcint0: pin change interrupt source 0. th e pb0 pin can serve as an external interrupt source for pin change interrupt 0. ? tpidata: serial programming data. ? port b, bit 1 ? adc1/ain1/cl ki/icp0/oc0b/pcint1/tpiclk ? adc1: analog to digital converter, channel 1 (attiny5/10, only) ? ain1: analog comparator negative input. configure the port pin as input with the internal pull-up switched off to avoid the digital port f unction from interfering with the function of the analog comparator. ? clki: external clock. ? icp0: input capture pin. the pb1 pin can act as an input capture pin for timer/counter0. table 10-3. port b pins alternate functions port pin alternate function pb0 adc0: adc input channel 0 ain0: analog comparator, positive input oc0a: timer/counter0 compare match a output pcint0: pin change interrupt 0, source 0 tpidata:serial programming data pb1 adc1: adc input channel 1 ain1: analog comparator, negative input clki: external clock icp0: timer/counter0 input capture input oc0b: timer/counter0 compare match b output pcint1:pin change interrupt 0, source 1 tpiclk: serial programming clock pb2 adc2: adc input channel 2 clko: system clock output int0: external interrupt 0 source pcint2: pin change interrupt 0, source 2 t0: timer/counter0 clock source pb3 adc3: adc input channel 3 pcint3: pin change interrupt 0, source 3 reset : reset pin
49 8127c?avr?10/09 attiny4/5/9/10 ? oc0b: output compare match output: the pb1 pi n can serve as an external output for the timer/counter0 compare match b. the pb1 pin has to be configured as an output (ddb1 set (one)) to serve this function. the oc0b pi n is also the output pin for the pwm mode timer function. ? pcint1: pin change interrupt source 1. th e pb1 pin can serve as an external interrupt source for pin change interrupt 0. ? tpiclk: serial programming clock. ? port b, bit 2 ? adc2 /clko/int0 /pcint2/t0 ? adc2: analog to digital converter, channel 2 (attiny5/10, only) ? clko: system clock output. the system clock ca n be output on pin pb2. the system clock will be output if ckout bit is programmed, regard less of the portb2 and ddb2 settings. ? int0: external interrupt request 0 ? pcint2: pin change interrupt source 2. th e pb2 pin can serve as an external interrupt source for pin change interrupt 0. ? t0: timer/counter0 counter source. ? port b, bit 3 ? adc3/pcint3/reset ? adc3: analog to digital converter, channel 3 (attiny5/10, only) ? pcint3: pin change interrupt source 3. th e pb3 pin can serve as an external interrupt source for pin change interrupt 0. ? reset : table 10-4 and table 10-5 on page 50 relate the alternate functions of port b to the overriding signals shown in figure 10-6 on page 46 . table 10-4. overriding signals for alternate functions in pb3..pb2 notes: 1. rstdisbl is 1 when the conf iguration bit is ?0? (programmed). 2. ckout is 1 when the configuration bit is ?0? (programmed). signal name pb3/adc3/reset /pcint3 pb2/adc2/int0/t0/clko/pcint2 puoe rstdisbl (1) ckout (2) puov 1 0 ddoe rstdisbl (1) ckout (2) ddov 0 1 pvoe 0 ckout (2) pvov 0 (system clock) ptoe 0 0 dieoe rstdisbl (1) + (pcint3 ? pcie0) + adc3d (pcint2 ? pcie0) + adc2d + int0 dieov rstdisbl ? pcint3 ? pc ie0 (pcint2 ? pcie0) + int0 di pcint3 input int0/t0/pcint2 input aio adc3 input adc2 input
50 8127c?avr?10/09 attiny4/5/9/10 notes: 1. ext_clock is 1 when external clock is selected as main clock. 10.4 register description 10.4.1 portcr ? port control register ? bits 7:2, 0 ? reserved these bits are reserved and will always read zero. ? bit 1 ? bbmb: break-before-make mode enable when this bit is set the break-before-make mode is activated for the entire port b. the interme- diate tri-state cycle is then inserted when writing ddrxn to make an output. for further information, see ?break-before-make switching? on page 42 . 10.4.2 pueb ? port b pull- up enable control register table 10-5. overriding signals for alternate functions in pb1..pb0 signal name pb1/adc1/ain1/oc0b/clki/icp0/pcint1 pb0/adc0/ain0/oc0a/pcint0 puoe ext_clock (1) 0 puov 0 0 ddoe ext_clock (1) 0 ddov 0 0 pvoe ext_clock (1) + oc0b enable oc0a enable pvov ext_clock (1) ? oc0b oc0a ptoe 0 0 dieoe ext_clock (1) + (pcint1 ? pcie0) + adc1d (pcint0 ? pcie0) + adc0d dieov (ext_clock (1) ? pwr_down ) + (ext_clock (1) ? pcint1 ? pcie0) pcint0 ? pcie0 di clock/icp0/pcint1 input pcint0 input aio adc1/analog comparator negative input adc0/analog comparator positive input bit 7 6 5 4 3 2 1 0 0x03 ? ? ? ? ? ? bbmb ?portcr read/write r r r r r r r/w r initial value 0 0 0 0 0 0 0 0 bit 76543210 0x03 ? ? ? ? pueb3 pueb2 pueb1 pueb0 pueb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
51 8127c?avr?10/09 attiny4/5/9/10 10.4.3 portb ? port b data register 10.4.4 ddrb ? port b da ta direction register 10.4.5 pinb ? po rt b input pins bit 76543210 0x02 ? ? ? ? portb3 portb2 portb1 portb0 portb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x01 ? ? ? ? ddb3 ddb2 ddb1 ddb0 ddrb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x00 ? ? ? ? pinb3 pinb2 pinb1 pinb0 pinb read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 n/a n/a n/a n/a
52 8127c?avr?10/09 attiny4/5/9/10 11. 16-bit timer/counter0 11.1 features ? true 16-bit design, including 16-bit pwm ? two independent output compare units ? double buffered output compare registers ? one input capture unit ? input capture noise canceler ? clear timer on compare match (auto reload) ? glitch-free, phase correct pulse width modulator (pwm) ? variable pwm period ? frequency generator ? external event counter ? four independent interrupt sources (tov0, ocf0a, ocf0b, and icf0) 11.2 overview the 16-bit timer/counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement. figure 11-1. 16-bit timer/counter block diagram clock s elect timer/counter data b u s ocrna ocrnb icrn = = tcntn waveform generation waveform generation ocna ocnb noise canceler icpn = fixed top values edge detector control logic = 0 top bottom count clear direction tovn (int.req.) ocna (int.req.) ocnb (int.req.) icfn (int.req.) tccrna tccrnb ( from analog comparator ouput ) tn edge detector ( from prescaler ) clk tn
53 8127c?avr?10/09 attiny4/5/9/10 a simplified block diagram of the 16 -bit timer/counter is shown in figure 11-1 on page 52 . for actual placement of i/o pins, refer to ?pinout of attiny4/5/9/10? on page 2 . cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bo ld. the device-specific i/o register and bit locations are listed in the ?register description? on page 73 . most register and bit references in this sect ion are written in general form. a lower case ?n? replaces the timer/counter number, and a lower case ?x? replaces the output compare unit channel. however, when using the register or bi t defines in a program, the precise form must be used, i.e., tcnt0 for accessing timer/counter0 counter value and so on. 11.2.1 registers the timer/counter (tcnt0), output compare re gisters (ocr0a/b), and input capture regis- ter (icr0) are all 16-bit registers. special proc edures must be followed when accessing the 16- bit registers. these procedures are described in the section ?accessing 16-bit registers? on page 71 . the timer/counter control r egisters (tccr0a/b) are 8-bit registers and have no cpu access restrictions. interrup t requests (abbreviated to int.req. in the figure) signals are all visible in the timer interrupt flag regi ster (tifr). all interrupts are in dividually masked with the timer interrupt mask register (timsk). tifr and timsk are not shown in the figure. the timer/counter can be clocked internally, via th e prescaler, or by an external clock source on the t0 pin. the clock select logic block contro ls which clock source and edge the timer/counter uses to increment (or decrement) its value. th e timer/counter is inactive when no clock source is selected. the output from th e clock select logic is referred to as the timer clock (clkt 0 ). the double buffered output compare registers (ocr0a/b) are compared with the timer/coun- ter value at all time. the result of the compare can be used by the waveform generator to generate a pwm or variable frequency output on the output compare pin (oc0a/b). see ?out- put compare units? on page 59 . the compare match event will also set the compare match flag (ocf0a/b) which can be used to generate an output compare interrupt request. the input capture register can c apture the timer/counter value at a given external (edge trig- gered) event on either the input capture pin (icp0) or on t he analog comparator pins (see ?analog comparator? on page 81 ). the input capture unit includes a digital filtering unit (noise canceler) for reducing the chance of capturing noise spikes. the top value, or maximum timer/counter valu e, can in some modes of operation be defined by either the ocr0a register, the icr0 regist er, or by a set of fixed values. when using ocr0a as top value in a pwm mode, the ocr0a register can not be used for generating a pwm output. however, the top value will in this case be do uble buffered allowing the top value to be changed in run time. if a fixed top value is required, the icr0 register can be used as an alternative, freeing the ocr0a to be used as pwm output. 11.2.2 definitions the following definitions are used ex tensively throughout the section: table 11-1. definitions constant description bottom the counter reaches bo ttom when it becomes 0x00 max the counter reaches its maximum wh en it becomes 0xff (decimal 255) top the counter reaches the top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be the fixed value 0xff (max) or the value stored in the ocr0a register. the assignment depends on the mode of operation
54 8127c?avr?10/09 attiny4/5/9/10 11.3 clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs02:0) bits located in the timer/counter control register b (tccr0b). for details on clock sources and prescaler, see section ?prescaler?. 11.3.1 prescaler the timer/counter can be clocked directly by the system clock (by setting the cs2:0 = 1). this provides the fastest operation, with a maximum timer/counter clock frequency equal to system clock frequency (f clk_i/o ). alternatively, one of four taps from the prescaler can be used as a clock source. see figure 11-2 for an illustration of the prescaler unit. figure 11-2. prescaler for timer/counter0 note: 1. the synchronization logic on the input pins ( t0) is shown in figure 11-3 on page 55 . the prescaled clock has a frequency of f clk_i/o /8, f clk_i/o /64, f clk_i/o /256, or f clk_i/o /1024. see table 11-6 on page 76 for details. 11.3.1.1 prescaler reset the prescaler is free running, i.e., operates independently of the clock select logic of the timer/countercounter, and it is shared by th e timer/counter tn. since the prescaler is not affected by the timer/counter?s cl ock select, the state of the pres caler will have implications for situations where a prescaled clock is used. one example of prescaling artifacts occurs when the timer is enabled and clocked by the prescaler (cs2:0 = 2, 3, 4, or 5). the number of system psr10 cle a r clk t0 t0 clk i/o synchroniz a tion
55 8127c?avr?10/09 attiny4/5/9/10 clock cycles from when the timer is enabled to the first count occurs can be from 1 to n+1 sys- tem clock cycles, where n equals the prescaler divisor (8, 64, 256, or 1024). it is possible to use the prescaler reset for synchronizing the timer/counter to program execution. 11.3.2 external clock source an external clock source appl ied to the t0 pin can be used as timer/counter clock (clk tn ). the tn pin is sampled once every system clock cycle by the pin synchronization logic. the synchro- nized (sampled) signal is then passed through the edge detector. figure 11-3 on page 55 shows a functional equivalent block diagram of the t0 synchronization and edge detector logic. the registers are clocked at the positive edge of the internal system clock ( clk i/o ). the latch is trans- parent in the high period of the internal system clock. the edge detector generates one clk t 0 pulse for each positive (cs2:0 = 7) or negative (cs2:0 = 6) edge it detects. figure 11-3. t0 pin sampling the synchronization and e dge detector logic introduces a de lay of 2.5 to 3.5 system clock cycles from an edge has been applied to the t0 pin to the counter is updated. enabling and disabling of the clock input must be done when t0 has been stable for at least one system clock cycle, otherwise it is a risk that a false timer/counter clock pulse is generated. each half period of the exter nal clock applied must be longer than one system clock cycle to ensure correct sampling. the external clock must be guaranteed to have less than half the sys- tem clock frequency (f extclk < f clk_i/o /2) given a 50/50% duty cycle. since the edge detector uses sampling, the maximum frequency of an external clock it can detect is half the sampling fre- quency (nyquist sampling theorem). however, due to variation of the system clock frequency and duty cycle caused by oscillator source (crystal, resonator , and capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than f clk_i/o /2.5. an external clock source can not be prescaled. 11.4 counter unit the main part of the 16-bit timer/counter is th e programmable 16-bit bi-directional counter unit. figure 11-4 on page 56 shows a block diagram of the counter and its surroundings. tn_sync (to clock select logic) edge detector synchroniz a tion dq dq le dq tn clk i/o
56 8127c?avr?10/09 attiny4/5/9/10 figure 11-4. counter unit block diagram signal description (internal signals): count increment or decrement tcnt0 by 1. direction select between increment and decrement. clear clear tcnt0 (set all bits to zero). clk t 0 timer/counter clock. top signalize that tcnt0 has reached maximum value. bottom signalize that tcnt0 has re ached minimum value (zero). the 16-bit counter is mapped into two 8-bit i/o memory locations: counter high (tcnt0h) con- taining the upper eight bits of the counter, an d counter low (tcnt0l) containing the lower eight bits. the tcnt0h register can only be indirect ly accessed by the cpu. when the cpu does an access to the tcnt0h i/o location, the cpu accesses the high byte temporary register (temp). the temporary register is updated with the tcnt0h value when the tcnt0l is read, and tcnt0h is updated with the temporary register va lue when tcnt0l is written. this allows the cpu to read or write the entire 16-bit counter value within one clock cycle via the 8-bit data bus. it is important to notice that there are special cases of writing to the tcnt0 register when the counter is counting that will gi ve unpredictable results. the s pecial cases are described in the sections where they are of importance. depending on the mode of operation used, the co unter is cleared, incremented, or decremented at each timer clock (clk t 0 ). the clk t 0 can be generated from an external or internal clock source, selected by the clock select bits (cs02:0). w hen no clock source is selected (cs02:0 = 0) the timer is stopped. however, the tcnt0 value can be accessed by the cpu, independent of whether clk t 0 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the setting of the waveform generation mode bits (wgm03:0) located in the timer/counter c ontrol registers a and b (tccr0a and tccr0b). there are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare outputs oc0x. for more details about advanced counting sequences and waveform generation, see ?modes of operation? on page 62 . the timer/counter overflow flag (tov0) is set a ccording to the mode of operation selected by the wgm03:0 bits. tov0 can be us ed for generating a cpu interrupt. temp ( 8 -bit) data b u s ( 8 -bit) tcntn (16-bit counter) tcntnh ( 8 -bit) tcntnl ( 8 -bit) control logic count clear direction tovn (int.req.) clock s elect top bottom tn edge detector ( from prescaler ) clk tn
57 8127c?avr?10/09 attiny4/5/9/10 11.5 input capture unit the timer/counter incorporates an input capture unit that can capture external events and give them a time-stamp indicating time of occurrence. the external signal indicating an event, or mul- tiple events, can be applied via the icp0 pin. the time-stamps can then be used to calculate frequency, duty-cycle, and other features of the signal applied. alternatively the time-stamps can be used for creating a log of the events. the input capture unit is illustrate d by the block diagram shown in figure 11-5 on page 57 . the elements of the block diagram that are not dire ctly a part of the input capture unit are gray shaded. the lower case ?n? in register and bit names indicates the timer/counter number. figure 11-5. input capture unit block diagram when a change of the logic level (an event) occurs on the input capture pin (icp0), alternatively on the analog comparator output (aco), and th is change confirms to the setting of the edge detector, a capture will be triggered. when a captur e is triggered, the 16-bit value of the counter (tcnt0) is written to the input capture register (icr0). the input capture flag (icf0) is set at the same system clock as the tcnt0 value is copi ed into icr0 register. if enabled (icie0 = 1), the input capture flag generates an input capt ure interrupt. the icf0 flag is automatically cleared when the interrupt is executed. alternativ ely the icf0 flag can be cleared by software by writing a logical one to its i/o bit location. reading the 16-bit value in the input capture register (icr0) is done by first reading the low byte (icr0l) and then the high byte (icr0h). when the low byte is read the high byte is copied into the high byte temporary regi ster (temp). when th e cpu reads the icr0h i/o location it will access the temp register. the icr0 register can only be written when using a waveform generation mode that utilizes the icr0 register for defining the counter?s top value. in these cases the waveform genera- icfn (int.req.) analog comparator write icrn (16-bit register) icrnh ( 8 -bit) noise canceler icpn edge detector temp ( 8 -bit) data b u s ( 8 -bit) icrnl ( 8 -bit) tcntn (16-bit counter) tcntnh ( 8 -bit) tcntnl ( 8 -bit) acic* icnc ice s aco*
58 8127c?avr?10/09 attiny4/5/9/10 tion mode (wgm03:0) bits must be set before the top value can be written to the icr0 register. when writing the icr0 re gister the high byte must be written to the icr0h i/o location before the low byte is written to icr0l. for more information on how to access the 16-bit registers refer to ?accessing 16-bit registers? on page 71 . 11.5.1 input capture trigger source the main trigger source for the input capture unit is the input capture pin (icp0). timer/counter0 can alternatively use the analog comparator output as trigger source for the input capture unit. the analog comparator is selected as trigger source by setting the analog comparator input capture (acic) bit in ?acsr ? analog comparator control and status regis- ter?. be aware that changing trigger source can trigger a capture. the input capture flag must therefore be cleared after the change. both the input capture pin (icp0) and the anal og comparator output (aco) inputs are sampled using the same technique as for the t0 pin ( figure 11-3 on page 55 ). the edge detector is also identical. however, when the noise canceler is enabled, additional logic is inserted before the edge detector, which increases t he delay by four system clock cycles. note that the input of the noise canceler and edge detector is always enabl ed unless the timer/counter is set in a wave- form generation mode that uses icr0 to define top. an input capture can be trigger ed by software by controlling the port of the icp0 pin. 11.5.2 noise canceler the noise canceler improves noise immunity by using a simple digita l filtering scheme. the noise canceler input is monitored over four samples, and all four must be equal for changing the output that in turn is used by the edge detector. the noise canceler is enabled by setting the input captur e noise canceler (icnc0) bit in timer/counter control register b (tccr0b). when enabled the noise canceler introduces addi- tional four system clock cycles of delay from a change applied to the input, to the update of the icr0 register. the noise canceler uses the sy stem clock and is therefore not affected by the prescaler. 11.5.3 using the input capture unit the main challenge when using the input capture unit is to assign enough processor capacity for handling the incoming events. the time between two events is critical. if the processor has not read the captured valu e in the icr0 register before th e next event occurs, the icr0 will be overwritten with a new value. in this case the result of the ca pture will be incorrect. when using the input capture in terrupt, the icr0 register shoul d be read as early in the inter- rupt handler routine as possible. even though the input capture interrupt has relatively high priority, the maximum interrupt response time is dependent on the maximum number of clock cycles it takes to handle any of the other interrupt requests. using the input capture unit in any mode of operation when the top value (resolution) is actively changed during operation, is not recommended. measurement of an external signal?s duty cycle requires that the trigger edge is changed after each capture. changing the edge sensing must be done as early as possible after the icr0 register has been read. after a change of the edge, the input capture flag (icf0) must be
59 8127c?avr?10/09 attiny4/5/9/10 cleared by software (writing a logical one to the i/o bit location). for measuring frequency only, the clearing of the icf0 flag is not r equired (if an interrupt handler is used). 11.6 output compare units the 16-bit comparator continuously compares tcnt0 with the output compare register (ocr0x). if tcnt equals ocr0x the comparator si gnals a match. a match will set the output compare flag (ocf0x) at the next timer clock c ycle. if enabled (ocie0x = 1), the output com- pare flag generates an output compare interrup t. the ocf0x flag is automatically cleared when the interrupt is executed. al ternatively the ocf0x flag can be cleared by software by writ- ing a logical one to its i/o bit location. the waveform generator uses the match signal to generate an output according to operating mode set by the waveform generation mode (wgm03:0) bits and compare output mode (c om0x1:0) bits. the to p and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation ( ?modes of operation? on page 62 ). a special feature of output comp are unit a allows it to define the timer/counter top value (i.e., counter resolution). in addition to the counter resolution, the top val ue defines the period time for waveforms generated by the waveform generator. figure 11-6 on page 59 shows a block diagram of the output compare unit. the small ?n? in the register and bit names indicates the device number (n = 0 for timer/counter 0), and the ?x? indi- cates output compare unit (a/b). the elements of the block diagram that are not directly a part of the output compare unit are gray shaded. figure 11-6. output compare unit, block diagram the ocr0x register is double buffered when us ing any of the twelve pulse width modulation (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the ocfnx (int.req.) = (16-bit comparator ) ocrnx buffer (16-bit register) ocrnxh buf. ( 8 -bit) ocnx temp ( 8 -bit) data b u s ( 8 -bit) ocrnxl buf. ( 8 -bit) tcntn (16-bit counter) tcntnh ( 8 -bit) tcntnl ( 8 -bit) comnx1:0 wgmn 3 :0 ocrnx (16-bit register) ocrnxh ( 8 -bit) ocrnxl ( 8 -bit) waveform generator top bottom
60 8127c?avr?10/09 attiny4/5/9/10 double buffering is disabled. the double buff ering synchronizes the update of the ocr0x com- pare register to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the out- put glitch-free. the ocr0x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr0x buffer register, and if double buffering is dis- abled the cpu will access the ocr0x directly. the content of the ocr0x (buffer or compare) register is only changed by a write operation (the timer/counter does not update this register automatically as the tcnt0 and icr0 register). therefore ocr0x is not read via the high byte temporary register (temp). however, it is a good practice to read the low byte first as when accessing other 16-bit registers. writing the oc r0x registers must be done via the temp reg- ister since the compare of all 16 bits is done continuously. the high byte (ocr0xh) has to be written first. when the high byte i/o location is written by the cpu, the temp register will be updated by the value written. then when the low by te (ocr0xl) is written to the lower eight bits, the high byte will be copied into the upper 8-bits of either the ocr0x bu ffer or ocr0x compare register in the same system clock cycle. for more information of how to acce ss the 16-bit registers refer to ?accessing 16-bit registers? on page 71 . 11.6.1 force output compare in non-pwm waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (0x) bit. forcing compare match will not set the ocf0x flag or reload/clear the timer, but the oc0x pin will be updated as if a real compare match had occurred (the com01:0 bits settings define whether the oc0x pin is set, cleared or toggled). 11.6.2 compare match blocking by tcnt0 write all cpu writes to the tcnt0 register will block any compare match that occurs in the next timer clock cycle, even when the timer is stopped. this feature allows ocr0x to be initialized to the same value as tcnt0 without triggering an inte rrupt when the timer/counter clock is enabled. 11.6.3 using the output compare unit since writing tcnt0 in any mode of operation will block all comp are matches for one timer clock cycle, there are risks involved when changi ng tcnt0 when using any of the output compare channels, independent of whether the timer/counter is running or not. if the value written to tcnt0 equals the ocr0x value, the compare matc h will be missed, resulting in incorrect wave- form generation. do not write the tcnt0 equal to top in pwm modes with variable top values. the compare match for the top will be ignored and the counte r will continue to 0xffff. similarly, do not write the tcnt0 value equal to bottom when the counter is downcounting. the setup of the oc0x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc0x value is to use the force output com- pare (0x) strobe bits in normal mode. the oc0x register keeps its value even when changing between waveform generation modes. be aware that the com0x1:0 bits are not doubl e buffered together with the compare value. changing the com0x1:0 bits will take effect immediately.
61 8127c?avr?10/09 attiny4/5/9/10 11.7 compare match output unit the compare output mode (com0x1:0) bits ha ve two functions. the waveform generator uses the com0x1:0 bits for defining the output co mpare (oc0x) state at the next compare match. secondly the com0x1:0 bits control the oc0x pin output source. figure 11-7 on page 61 shows a simplified schematic of the logic affected by the com0x1:0 bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold . only the parts of the general i/o port control registers (ddr and port) that are affected by the com0x1:0 bits are shown. when referring to the oc0x state, the reference is for the internal oc0x register , not the oc0x pin. if a system reset occur, the oc0x register is reset to ?0?. figure 11-7. compare match output unit, schematic (non-pwm mode) the general i/o port function is overridden by the output compare (oc0x) from the waveform generator if either of the com0x1:0 bits are se t. however, the oc0x pin direction (input or out- put) is still controlled by the da ta direction register (ddr) for th e port pin. the data direction register bit for the oc0x pin (ddr_oc0x) must be set as output before th e oc0x value is visi- ble on the pin. the port overri de function is generally indep endent of the waveform generation mode, but there are some exceptions. see table 11-2 on page 74 , table 11-3 on page 74 and table 11-4 on page 74 for details. the design of the output compare pin logic allows initialization of the oc 0x state before the out- put is enabled. note that some com0x1:0 bi t settings are reserved for certain modes of operation. see ?register description? on page 73 the com0x1:0 bits have no effect on the input capture unit. port ddr dq dq ocnx pin ocnx dq waveform generator comnx1 comnx0 0 1 data b u s focnx clk i/o
62 8127c?avr?10/09 attiny4/5/9/10 11.7.1 compare output mode and waveform generation the waveform generator uses the com0x1:0 bits differently in normal, ctc, and pwm modes. for all modes, setting the com0x1:0 = 0 tell s the waveform generator that no action on the oc0x register is to be performed on the next compare match. for compare output actions in the non-pwm modes refer to table 11-2 on page 74 . for fast pwm mode refer to table 11-3 on page 74 , and for phase correct and phase and frequency correct pwm refer to table 11-4 on page 74 . a change of the com0x1:0 bits st ate will have effect at the first compare match after the bits are written. for non-pwm modes, the action can be forced to have immediate effect by using the 0x strobe bits. 11.8 modes of operation the mode of operation, i.e., t he behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm03:0) and compare output mode (com0x1:0) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com0x1:0 bits control whether the pwm out- put generated should be inverted or not (inverted or non-inverted pwm). for non-pwm modes the com0x1:0 bits control whether the output should be set, cleared or toggle at a compare match ( ?compare match output unit? on page 61 ) for detailed timing information refer to ?timer/counter timing diagrams? on page 69 . 11.8.1 normal mode the simplest mode of operation is the normal mode (wgm03:0 = 0). in this mode the counting direction is always up (incre menting), and no counter clear is performed. the counter simply overruns when it passes its maximum 16-bit value (max = 0xffff) and then restarts from the bottom (0x0000). in normal operation the timer/c ounter overflow flag (tov0) will be set in the same timer clock cycle as the tcnt0 beco mes zero. the tov0 flag in this case behaves like a 17th bit, except that it is only set, not cleared. however, combined with the timer overflow interrupt that automatically clears the tov0 flag , the timer resolution ca n be increased by soft- ware. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the input capture unit is easy to use in normal mode. however, observe that the maximum interval between the external events must not exceed the resolution of the counter. if the interval between events are too long, the timer overflow interrupt or the prescaler must be used to extend the resolution for the capture unit. the output compare units can be used to generat e interrupts at some given time. using the output compare to gene rate waveforms in norm al mode is not recommended, since this will occupy too much of the cpu time. 11.8.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm03:0 = 4 or 12), the ocr0a or icr0 register are used to manipulate the counter resolution. in ctc mode the counter is cleared to zero when the counter value (tcnt0) matches either the oc r0a (wgm03:0 = 4) or the icr0 (wgm03:0 = 12). the ocr0a or icr0 define the top value for the counter, hence also its resolution. this mode allows greater control of the compare match ou tput frequency. it also simplifies the opera- tion of counting external events.
63 8127c?avr?10/09 attiny4/5/9/10 the timing diagram for the ctc mode is shown in figure 11-8 on page 63 . the counter value (tcnt0) increases until a compare match occurs with either ocr0a or icr0, and then counter (tcnt0) is cleared. figure 11-8. ctc mode, timing diagram an interrupt can be generated at each time the counter value reaches the top value by either using the ocf0a or icf0 flag according to the re gister used to define the top value. if the inter- rupt is enabled, the interrupt handler routine ca n be used for updating the top value. however, changing the top to a value close to bottom w hen the counter is running with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written to ocr0a or icr0 is lowe r than the current value of tcnt0, the counter will miss the compare matc h. the counter will th en have to count to its maximum value (0xffff) and wrap around starting at 0x0000 before the compare match can occur. in many cases this feature is not desirable. an alternativ e will then be to use the fast pwm mode using ocr0a for defining top (wgm03:0 = 15) si nce the ocr0a then will be double buffered. for generating a waveform output in ctc mode, t he oc0a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com0a1:0 = 1). the oc0a value will not be visible on the port pin unless the data direction for the pin is set to output (ddr_oc0a = 1). th e waveform generated will have a maximum fre- quency of 0 a = f clk_i/o /2 when ocr0a is set to zero (0x0000). the waveform frequency is defined by the following equation: the n variable represents the prescaler factor (1, 8, 64, 256, or 1024). as for the normal mode of operation, the tov0 flag is set in the same time r clock cycle that the counter counts from max to 0x0000. 11.8.3 fast pwm mode the fast pulse width modulation or fast pwm mode (wgm03:0 = 5, 6, 7, 14, or 15) provides a high frequency pwm waveform generation option. the fast pwm differs from the other pwm options by its single-slope operation. the counter counts from bottom to top then restarts from bottom. in non-inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x, and set at bottom. in inverting compare output mode output is set on compare match and cleared at bottom. due to the single-slope tcntn ocna (toggle) ocna interrupt flag s et or icfn interrupt flag s et (interrupt on top) 1 4 period 2 3 (comna1:0 = 1) f ocna f clk_i/o 2 n 1 ocrna + () ?? --------------------------------------------------- =
64 8127c?avr?10/09 attiny4/5/9/10 operation, the operating frequency of the fast pwm mode can be twice as high as the phase cor- rect and phase and frequency correct pwm modes that use dual-slope operation. this high frequency makes the fast pwm mode well suited for power regula tion, rectification, and dac applications. high frequency allows physically sm all sized external com ponents (coils, capaci- tors), hence reduces total system cost. the pwm resolution for fast pwm can be fixed to 8- , 9-, or 10-bit, or defined by either icr0 or ocr0a. the minimum resolution allowed is 2-bit (icr0 or ocr0a set to 0x0003), and the max- imum resolution is 16-bit (icr0 or ocr0a set to max). the pwm resolution in bits can be calculated by using the following equation: in fast pwm mode the co unter is incremented until the counter value matches either one of the fixed values 0x00ff, 0x01ff, or 0x03ff (wgm03:0 = 5, 6, or 7), the value in icr0 (wgm03:0 = 14), or the value in ocr0a (wgm03:0 = 15). th e counter is then cleared at the following timer clock cycle. the timing diagram fo r the fast pwm mode is shown in figure 11-9 on page 64 . the figure shows fast pwm mode when ocr0a or icr0 is used to define top. the tcnt0 value is in the timing diagram shown as a histogram for illustrating the single-slope operation. the dia- gram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent compare matches between ocr0x and tcnt0. the oc0x interrupt flag will be set when a compare match occurs. figure 11-9. fast pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches top. in addition the oc0a or icf0 flag is set at the same timer clock cycle as tov0 is set when either ocr0a or icr0 is used for defining the top value. if one of the interrupts are enabled, the interrupt han- dler routine can be used for updating the top and compare values. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will never occur between the tcnt0 and the ocr0x. note that when using fixed top values the unused bits are masked to zero when any of the ocr0x registers are written. r fpwm top 1 + () log 2 () log ----------------------------------- = tcntn ocrnx/top update and tovn interrupt flag set and ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 7 period 2 3 4 5 6 8 ocnx ocnx (comnx1:0 = 2) (comnx1:0 = 3)
65 8127c?avr?10/09 attiny4/5/9/10 the procedure for updating icr0 differs from updating ocr0a when used for defining the top value. the icr0 register is not double buffered. this means that if icr0 is changed to a low value when the counter is running with none or a lo w prescaler value, there is a risk that the new icr0 value written is lower than the current va lue of tcnt0. the result will then be that the counter will miss the compare matc h at the top value. the counter will then have to count to the max value (0xffff) and wrap around starting at 0x0000 before the compare match can occur. the ocr0a register however, is double buffered. this feature allows the ocr0a i/o location to be written anytime. when the ocr0a i/o location is written the value written will be put into the ocr0a buffer register. th e ocr0a compare register will th en be updated with the value in the buffer register at the next timer clo ck cycle the tcnt0 matches top. the update is done at the same timer clock cycle as the tcnt 0 is cleared and the tov0 flag is set. using the icr0 register for defining top work s well when using fixed top values. by using icr0, the ocr0a register is free to be used for generating a pwm output on oc0a. however, if the base pwm frequency is actively change d (by changing the top value), using the ocr0a as top is clearly a better choice due to its double buffer feature. in fast pwm mode, the compare units allow generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com0x1:0 to three (see table 11-3 on page 74 ). the actual oc0x value will only be visible on the port pin if th e data direction for the po rt pin is set as output (ddr_oc0x). the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0, and clearing (or setting) the oc0x register at the timer clock cycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr0x register re presents special cases when generating a pwm waveform output in the fast pwm mode. if the ocr0x is set equal to bottom (0x0000) the out- put will be a narrow spike for eac h top+1 timer clock cycle. se tting the ocr0x equal to top will result in a const ant high or low output (depending on the polarity of the output set by the com0x1:0 bits.) a frequency (with 50% duty cycle) waveform out put in fast pwm mode can be achieved by set- ting oc0a to toggle its logical level on each compare match (com0a1:0 = 1). the waveform generated will have a ma ximum frequency of f 0 a = f clk_i/o /2 when ocr0a is se t to zero (0x0000). this feature is similar to the oc 0a toggle in ctc mode, except the double buffer feature of the output compare unit is enabled in the fast pwm mode. 11.8.4 phase correct pwm mode the phase correct pulse width modulation or phase correct pwm mode (wgm03:0 = 1, 2, 3, 10, or 11) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is, like the phase and frequency correct pwm mode, based on a dual- slope operation. the counter counts repeatedly from bottom (0x0000) to top and then from top to bottom. in non-inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x while upcounting, and set on the compare match while downcounting. in inverting output compare mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope f ocnxpwm f clk_i/o n1top + () ? ----------------------------------- =
66 8127c?avr?10/09 attiny4/5/9/10 operation. however, due to the symmetric feat ure of the dual-slope pwm modes, these modes are preferred for motor control applications. the pwm resolution for the phase correct pwm mode can be fixed to 8-, 9-, or 10-bit, or defined by either icr0 or ocr0a. the minimum resolution allowed is 2-bit (icr0 or ocr0a set to 0x0003), and the maximum resolution is 16-bit (icr0 or ocr0a set to max). the pwm resolu- tion in bits can be calculated by using the following equation: in phase correct pwm mode the counter is incr emented until the counter value matches either one of the fixed values 0x00ff, 0x01ff, or 0x 03ff (wgm03:0 = 1, 2, or 3), the value in icr0 (wgm03:0 = 10), or the value in ocr0a (wgm03:0 = 11). the counter has then reached the top and changes the count direct ion. the tcnt0 value will be equa l to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 11-10 on page 66 . the figure shows phase correct pwm mode when ocr0a or icr0 is used to define top. the tcnt0 value is in the timing diagram shown as a histogram for illu strating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent compare matches between ocr0x and tcnt0. the oc0x interrupt flag will be set when a compare match occurs. figure 11-10. phase correct pwm mode, timing diagram the timer/counter overflow flag (tov0) is se t each time the counter reaches bottom. when either ocr0a or icr0 is used for defining the to p value, the oc0a or icf0 flag is set accord- ingly at the same timer clock cycle as the ocr0x registers are updated with the double buffer value (at top). the interr upt flags can be used to generate an interrupt each time the counter reaches the top or bottom value. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the r pcpwm top 1 + () log 2 () log ----------------------------------- = ocrnx/top update and ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 2 3 4 tovn interrupt flag set (interrupt on bottom) tcntn period ocnx ocnx (comnx1:0 = 2) (comnx1:0 = 3)
67 8127c?avr?10/09 attiny4/5/9/10 compare registers, a compare match will never occur between the tcnt0 and the ocr0x. note that when using fixed top values, the unus ed bits are masked to zero when any of the ocr0x registers are written. as the third period shown in figure 11-10 on page 66 illustrates, changing the top actively while the timer/counter is running in the phase correct mode can result in an unsymmetrical output. the reason for this can be found in the time of update of the ocr0x register. since the ocr0x update occurs at top, the pwm period starts and ends at top. this implies that the length of the falling slope is determined by the previous top value, while the length of the rising slope is determin ed by the new top value. when these two values differ the two slopes of the period will differ in length. the differ ence in length gives the unsym- metrical result on the output. it is recommended to use the phase and frequency correct mode instead of the phase correct mode when changing the top value while the timer/counter is running. when using a static top value there are practically no differ ences between the two modes of operation. in phase correct pwm mode, the compare units allow generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to tw o will produce a non-inverte d pwm and an inverted pwm output can be generated by setting the com0x1:0 to three (see table 11-4 on page 74 ). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output (ddr_oc0x). the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0 when the counter increments, and clearing (or setting) the oc0x register at compare match between ocr0x and tcnt0 when the counter decrements. the pw m frequency for the output when using phase correct pwm can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr0x register re present special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr0x is set equal to bottom the output will be continuously low and if set equal to top the output will be continuously high for non-inverted pwm mode. for in verted pwm the output will have the opposite logic values. 11.8.5 phase and frequency correct pwm mode the phase and frequency correct pulse width modulation, or phase and frequency correct pwm mode (wgm03:0 = 8 or 9) provides a high reso lution phase and frequency correct pwm wave- form generation option. the phase and frequency correct pwm mode is, like the phase correct pwm mode, based on a dual-slope operation. the counter counts repeatedly from bottom (0x0000) to top and then from top to bottom. in non-inverting compare output mode, the output compare (oc0x) is cleared on the co mpare match between tcnt0 and ocr0x while upcounting, and set on the compare match while downcounting. in inverting compare output mode, the operation is inverted. the dual-slope operation gives a lower maximum operation fre- quency compared to the single-slope operation. howe ver, due to the symme tric feature of the dual-slope pwm modes, these modes are preferred for motor control applications. the main difference between the phase correct, and the phase and frequency correct pwm mode is the time the ocr0x register is up dated by the ocr0x buffer register, (see figure 11- 10 on page 66 and figure 11-11 on page 68 ). the pwm resolution for the phase and frequency correct pwm mode can be defined by either icr0 or ocr0a. the minimum resolution allowed is 2-bit (icr0 or ocr0a set to 0x0003), and f ocnxpcpwm f clk_i/o 2ntop ?? ---------------------------- - =
68 8127c?avr?10/09 attiny4/5/9/10 the maximum resolution is 16-bit (icr0 or ocr0 a set to max). the pwm re solution in bits can be calculated using the following equation: in phase and frequency correct pwm mode the counter is incremented until the counter value matches either the value in icr0 (wgm03:0 = 8), or the value in ocr0a (wgm03:0 = 9). the counter has then reac hed the top and ch anges the count di rection. the tcnt0 value will be equal to top for one timer clock cycle. the timi ng diagram for the phase correct and frequency correct pwm mode is shown on figure 11-11 on page 68 . the figure shows phase and fre- quency correct pwm mode when ocr0a or icr0 is used to define top. the tcnt0 value is in the timing diagram shown as a histogram for il lustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent compare matc hes between ocr0x and tcnt0. the oc0x interrupt flag will be set when a compare match occurs. figure 11-11. phase and frequency correct pwm mode, timing diagram the timer/counter overflow flag (tov0) is set at the same timer cloc k cycle as the ocr0x registers are updated with the double buffer value (at bottom). when either ocr0a or icr0 is used for defining the top value, the oc0a or icf0 flag set when tcnt0 has reached top. the interrupt flags can then be used to generate an interrupt each time the counter reaches the top or bottom value. when changing the top value the program must ensure that the new top value is higher or equal to the value of all of the compare registers. if the top value is lower than any of the compare registers, a compare match will neve r occur between the tcnt0 and the ocr0x. as figure 11-11 on page 68 shows the output generated is, in contrast to the phase correct mode, symmetrical in all periods. since th e ocr0x registers are updated at bottom, the length of the rising and the falling slopes wi ll always be equal. this gives symmetrical output pulses and is therefore frequency correct. r pfcpwm top 1 + () log 2 () log ----------------------------------- = ocrnx/top updateand tovn interrupt flag set (interrupt on bottom) ocna interrupt flag set or icfn interrupt flag set (interrupt on top) 1 2 3 4 tcntn period ocnx ocnx (comnx1:0 = 2) (comnx1:0 = 3)
69 8127c?avr?10/09 attiny4/5/9/10 using the icr0 register for defining top work s well when using fixed top values. by using icr0, the ocr0a register is free to be used for generating a pwm output on oc0a. however, if the base pwm frequency is actively change d by changing the top value, using the ocr0a as top is clearly a better choice due to its double buffer feature. in phase and frequency correct pwm mode, the compare units allow generation of pwm wave- forms on the oc0x pins. settin g the com0x1:0 bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com0x1:0 to three (see table 11-4 on page 74 ). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output (ddr_oc0x). the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0 when the counter incre- ments, and clearing (or setting) the oc0x register at compare match between ocr0x and tcnt0 when the counter decrem ents. the pwm frequency for the output when using phase and frequency correct pwm can be calculated by the following equation: the n variable represents the prescaler divider (1, 8, 64, 256, or 1024). the extreme values for the ocr0x register re presents special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr0x is set equal to bottom the output will be continuously low and if set equal to top the output will be set to high for non- inverted pwm mode. for inverted pwm the ou tput will have the opposite logic values. 11.9 timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t0 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set, and when the ocr0x register is updated with the ocr0x buffer value (only for modes utilizing double buffering). figure 11-12 on page 69 shows a timing diagram for the set- ting of ocf0x. figure 11-12. timer/counter timing diagram, setting of ocf0x, no prescaling f ocnxpfcpwm f clk_i/o 2ntop ?? ---------------------------- - = clk tn (clk i/o /1) ocfnx clk i/o ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2
70 8127c?avr?10/09 attiny4/5/9/10 figure 11-13 on page 70 shows the same timing data, but with the prescaler enabled. figure 11-13. timer/counter timing diagram, setting of ocf0x, with prescaler (f clk_i/o /8) figure 11-14 on page 70 shows the count sequence close to top in various modes. when using phase and frequency correct pwm mode t he ocr0x register is updated at bottom. the timing diagrams will be the same, but to p should be replaced by bottom, top-1 by bottom+1 and so on. the same renaming applies for modes that set the tov0 flag at bottom. figure 11-14. timer/counter timing diagram, no prescaling figure 11-15 on page 71 shows the same timing data, but with the prescaler enabled. ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) tovn (fpwm) and icfn (if used as top) ocrnx (update at top) tcntn (ctc and fpwm) tcntn (pc and pfc pwm) top - 1 top top - 1 top - 2 old ocrnx value new ocrnx value top - 1 top bottom bottom + 1 clk tn (clk i/o /1) clk i/o
71 8127c?avr?10/09 attiny4/5/9/10 figure 11-15. timer/counter timing diagram, with prescaler (f clk_i/o /8) 11.10 accessing 16-bit registers the tcnt0, ocr0a/b, and icr0 are 16-bit regist ers that can be accessed by the avr cpu via the 8-bit data bus. the 16-bit register must be byte accessed using two read or write operations. each 16-bit timer has a single 8-bit register for temporary storing of the high byte of the 16-bit access. the same temporary register is shared between all 16-bit registers within each 16-bit timer. accessing the low byte triggers the 16-bit read or write operation. when the low byte of a 16-bit register is written by the cpu, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit regist er in the same clock cycle. when the low byte of a 16-bit register is read by the cpu, the high by te of the 16-bit register is copied into the tempo- rary register in the same clock cycle as the low byte is read. not all 16-bit accesses uses the temporary regi ster for the high byte. reading the ocr0a/b 16- bit registers does not involve using the temporary register. to do a 16-bit write, the high byte must be written before the low byte. for a 16-bit read, the low byte must be read before the high byte. the following code example shows how to access the 16-bit timer registers assuming that no interrupts updates the temporary register. the same principle can be used directly for accessing the ocr0a/b and icr0 registers. tovn (fpwm) and icf n (if used as top) ocrnx (update at top) tcntn (ctc and fpwm) tcntn (pc and pfc pwm) top - 1 top top - 1 top - 2 old ocrnx value new ocrnx value top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o /8)
72 8127c?avr?10/09 attiny4/5/9/10 note: see ?code examples? on page 5 . the code example returns the tcnt0 value in the r17:r16 register pair. it is important to notice that accessing 16-bit registers are atomic operations. if an interrupt occurs between the two instructions accessing the 16-bit register, and the interrupt code updates the temporary register by accessing the sa me or any other of the 16-bit timer registers, then the result of the access outside the interrupt will be corrupted. therefore, when both the main code and the interrupt code update the temporary register, the main code must disable the interrupts during the 16-bit access. the following code example shows how to do an atomic read of the tcnt0 register contents. reading any of the ocr0a/b or icr0 register s can be done by using the same principle. note: see ?code examples? on page 5 . the code example returns the tcnt0 value in the r17:r16 register pair. the following code example shows how to do an at omic write of the tcnt0 register contents. writing any of the ocr0a/b or icr0 register s can be done by using the same principle. assembly code example ... ; set tcnt 0 to 0x01ff ldi r17,0x01 ldi r16,0xff out tcnt 0 h,r17 out tcnt 0 l,r16 ; read tcnt 0 into r17:r16 in r16,tcnt 0 l in r17,tcnt 0 h ... assembly code example tim16_readtcnt 0 : ; save global interrupt flag in r18,sreg ; disable interrupts cli ; read tcnt 0 into r17:r16 in r16,tcnt 0 l in r17,tcnt 0 h ; restore global interrupt flag out sreg,r18 ret
73 8127c?avr?10/09 attiny4/5/9/10 note: see ?code examples? on page 5 . the code example requires that the r17:r16 regi ster pair contains the value to be written to tcnt0. 11.10.1 reusing the temporary high byte register if writing to more than one 16-bit register where the high byte is the same for all registers written, then the high byte only needs to be written once. however, note that the same rule of atomic operation described previously also applies in this case. 11.11 register description 11.11.1 tccr0a ? timer/counter0 control register a ? bits 7:6 ? com0a1:0: compare output mode for channel a ? bits 5:4 ? com0b1:0: compare output mode for channel b the com0a1:0 and com0b1:0 control the behaviour of output compare pins oc0a and oc0b, respectively. if one or bo th com0a1:0 bits are written to one, the oc0a output overrides the normal port functionality of the i/o pin it is connected to. similarly, if one or both com0b1:0 bit are written to one, the oc0b output overrides t he normal port functiona lity of the i/o pin it is connected to. note, however, that the data direction register (ddr) bit corresponding to the oc0a or oc0b pin must be set in order to enable the output driver. assembly code example tim16_writetcnt 0 : ; save global interrupt flag in r18,sreg ; disable interrupts cli ; set tcnt 0 to r17:r16 out tcnt 0 h,r17 out tcnt 0 l,r16 ; restore global interrupt flag out sreg,r18 ret bit 7 6 5 4 3 2 1 0 0x2e com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 tccr0a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0
74 8127c?avr?10/09 attiny4/5/9/10 when oc0a or oc0b is connected to the pin, the function of com0x1:0 bits depends on the wgm03:0 bits. table 11-2 shows the com0x1:0 bit functiona lity when the wgm03:0 bits are set to a normal or ctc (non-pwm) mode. table 11-3 shows the com0x1:0 bit functionality when the wgm03:0 bits are set to one of the fast pwm modes. note: 1. a special case occurs when ocr0a/oc r0b equals top and com0a1/com0b1 is set. in this case the compare match is ignored, but set or clear is done at bottom. see ?fast pwm mode? on page 63 for more details. table 11-4 shows the com0x1:0 bit functionality wh en the wgm03:0 bits are set to the phase correct or the phase and frequency correct, pwm mode. note: 1. a special case occurs when ocr0a/ ocr0b equals top and com0a1/com0b1 is set. ?phase correct pwm mode? on page 65 for more details. table 11-2. compare output in non-pwm modes com0a1/ com0b1 com0a0 com0b0 description 0 0 normal port operation: oc0a/oc0b disconnected 1 toggle oc0a/oc0b on compare match 1 0 clear (set low) oc0a/oc0b on compare match 1 set (high) oc0a/oc0b on compare match table 11-3. compare output in fast pwm modes com0a1/ com0b1 com0a0/ com0b0 description 0 0 normal port operation: oc0a/oc0b disconnected 1 wgm03 = 0: normal port oper ation, oc0a/oc0b disconnected wgm03 = 1: toggle oc0a on compare match, oc0b reserved 1 (1) 0 clear oc0a/oc0b on compare match set oc0a/oc0b at bottom (non-inverting mode) 1 set oc0a/oc0b on compare match clear oc0a/oc0b at bottom (inverting mode) table 11-4. compare output in phase correct and phase & frequency correct pwm modes com0a1/ com0b1 com0a0/ com0b0 description 0 0 normal port operation: oc0a/oc0b disconnected. 1 wgm03 = 0: normal port oper ation, oc0a/oc0b disconnected wgm03 = 1: toggle oc0a on compare match, oc0b reserved 1 (1) 0 counting up: clear oc0a/oc0b on compare match counting down: set oc0a/oc0b on compare match 1 counting up: set oc0a/oc0b on compare match counting down: clear oc0a/oc0b on compare match
75 8127c?avr?10/09 attiny4/5/9/10 ? bits 1:0 ? wgm01:0: waveform generation mode combined with wgm03:2 bits of tccr0b, these bits control the counting sequence of the coun- ter, the source for maximum (top) counter value, and what type of waveform to generate. see table 11-5 . modes of operation supported by the timer/counter unit are: normal mode (coun- ter), clear timer on compare match (ctc) mode, and three types of pulse width modulation (pwm) modes. ( ?modes of operation? on page 62 ). 11.11.2 tccr0b ? timer/counter0 control register b ? bit 7 ? icnc0: input capture noise canceler setting this bit (to one) activates the input capt ure noise canceler. when the noise canceler is activated, the input from the inpu t capture pin (icp0) is filtered. the filter function requires four successive equal valued samples of the icp0 pin for changing its output. the input capture is therefore delayed by four oscillator cycles when the noise canceler is enabled. ? bit 6 ? ices0: input capture edge select this bit selects which edge on the input capture pin (icp0) that is used to trigger a capture event. when the ices0 bit is written to zero, a falling (negative) edge is used as trigger, and when the ices0 bit is written to one, a risi ng (positive) edge w ill trigger the capture. table 11-5. waveform generation modes mode wgm 0 3:0 mode of operation top update of ocr0 x at tov0 flag set on 0 0000 normal 0xffff immediate max 1 0001 pwm, phase correct, 8-bit 0x00ff top bottom 2 0010 pwm, phase correct, 9-bit 0x01ff top bottom 3 0011 pwm, phase correct, 10-bit 0x03ff top bottom 40100ctc ( clear timer on compare ) ocr0a immediate max 5 0101 fast pwm, 8-bit 0x00ff top top 6 0110 fast pwm, 9-bit 0x01ff top top 7 0111 fast pwm, 10-bit 0x03ff top top 8 1000 pwm, phase & freq. correct icr0 bottom bottom 9 1001 pwm, phase & freq. correct ocr0a bottom bottom 10 1010 pwm, phase correct icr0 top bottom 11 1011 pwm, phase correct ocr0a top bottom 12 1100 ctc ( clear timer on compare ) icr0 immediate max 13 1101 (reserved) ? ? ? 14 1110 fast pwm icr0 top top 15 1111 fast pwm ocr0a top top bit 7654 3210 0x2d icnc0 ices0 ? wgm03 wgm02 cs02 cs01 cs00 tccr0b read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
76 8127c?avr?10/09 attiny4/5/9/10 when a capture is triggered according to the ices0 setting, the counter value is copied into the input capture register (icr0). the event will also set the input capture flag (icf0), and this can be used to cause an input capture in terrupt, if this in terrupt is enabled. when the icr0 is used as top value (see description of the wgm03:0 bits located in the tccr0a and the tccr0b register), the icp0 is disconnected and consequently the input cap- ture function is disabled. ? bit 5 ? reserved bit this bit is reserved for future use. for ensuring compatibility with future de vices, this bit must be written to zero when tccr0b is written. ? bits 4:3 ? wgm03:2: waveform generation mode see ?tccr0a ? timer/counter0 control register a? on page 73 . ? bits 2:0 ? cs02:0: clock select the three clock select bits set the clock source to be used by the timer/counter, see figure 11- 12 and figure 11-13 . if external pin modes are used for the timer/counter0, transitions on the t0 pin will clock the counter even if the pin is configured as an outpu t. this feature allows software control of the counting. 11.11.3 tccr0c ? timer/counter0 control register c ? bit 7 ? foc0a: force output compare for channel a ? bit 6 ? foc0b: force output compare for channel b the foc0a/foc0b bits are only active when the wgm03:0 bits specifies a non-pwm mode. however, for ensuring compatibilit y with future devices, these bits must be set to zero when tccr0a is written when operating in a pw m mode. when writing a logical one to the foc0a/foc0b bit, an immediate compare matc h is forced on the wave form generation unit. table 11-6. clock select bit description cs02 cs01 cs00 description 0 0 0 no clock source (timer/counter stopped) 001clk i/o /1 (no prescaling) 010clk i/o /8 (from prescaler) 011clk i/o /64 (from prescaler) 100clk i/o /256 (from prescaler) 101clk i/o /1024 (from prescaler) 1 1 0 external clock source on t0 pin. clock on falling edge 1 1 1 external clock source on t0 pin. clock on rising edge bit 7654 3210 0x2c foc0a foc0b ? ? ? ? ? ? tccr0c read/write w w r r r r r r initial value 0 0 0 0 0 0 0 0
77 8127c?avr?10/09 attiny4/5/9/10 the oc0a/oc0b output is changed according to its com0x1:0 bits setting. note that the foc0a/foc0b bits are implemented as strobes. therefore it is the value present in the com0x1:0 bits that determine t he effect of the forced compare. a foc0a/foc0b strobe will not generate any interrupt nor will it clear t he timer in clear timer on compare match (ctc) mode using ocr0a as top. the foc0a/foc0b bits are always read as zero. ? bits 5:0 ? reserved bits these bits are reserved for future use. for ensuring compatibility with future devices, these bits must be written to zero when the register is written. 11.11.4 tcnt0h and tcnt0l ? timer/counter0 the two timer/counter i/o locations (tcnt0 h and tcnt0l, combined tcnt0) give direct access, both for read and for write operations, to the timer/counter unit 16-bit counter. to ensure that both the high and low bytes are read and written simultaneously when the cpu accesses these registers, the access is perfo rmed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. see ?accessing 16-bit registers? on page 71 . modifying the counter (tcnt0) while the counte r is running introduces a risk of missing a com- pare match between tcnt0 and one of the ocr0x registers. writing to the tcnt0 register blocks (removes ) the compare match on the following timer clock for all compare units. 11.11.5 ocr0ah and ocr0al ? ou tput compare register 0 a 11.11.6 ocr0bh and ocr0bl ? ou tput compare register 0 b the output compare registers contain a 16-bit value that is continuo usly compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0x pin. the output compare registers are 16-bit in size. to ensure that both the high and low bytes are written simultaneously when the cp u writes to these registers, the access is performed using an bit 76543210 0x29 tcnt0[15:8] tcnt0h 0x28 tcnt0[7:0] tcnt0l read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x27 ocr1a[15:8] ocr0ah 0x26 ocr1a[7:0] ocr0al read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x25 ocr0b[15:8] ocr0bh 0x24 ocr0b[7:0] ocr0bl read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
78 8127c?avr?10/09 attiny4/5/9/10 8-bit temporary high byte register (temp). this te mporary register is shared by all the other 16- bit registers. see ?accessing 16-bit registers? on page 71 . 11.11.7 icr0h and icr0l ? input capture register 0 the input capture is updated with the counter (tcnt0) value each time an event occurs on the icp0 pin (or optionally on the analog comparator output for timer/counter0). the input capture can be used for defining the counter top value. the input capture register is 16-bit in size. to ensure that both the high and low bytes are read simultaneously when the cpu accesses these regi sters, the access is performed using an 8-bit temporary high byte register (temp). this temporary register is shared by all the other 16-bit registers. ?accessing 16-bit registers? on page 71 . 11.11.8 timsk0 ? timer/counter interrupt mask register 0 ? bits 7:6, 4:3 ? reserved bits these bits are reserved for future use. for ensuring compatibility with future devices, these bits must be written to zero when the register is written. ? bit 5 ? icie0: timer/counter0, input capture interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter0 input capture interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 66.) is executed when the icf0 flag, located in tifr0, is set. ? bit 2 ? ocie0b: timer/counter0, output compare b match interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter0 output compare b match interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 35 ) is executed when the ocf0b flag, located in tifr0, is set. ? bit 1 ? ocie0a: timer/counter0, output compare a match interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter0 output compare a match interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 35 ) is executed when the ocf0a flag, located in tifr0, is set. ? bit 0 ? toie0: timer/counter0, overflow interrupt enable when this bit is written to one, and the i-flag in the status register is set (interrupts globally enabled), the timer/counter0 overflow interrupt is enabled. the corresponding interrupt vector (see ?interrupts? on page 35 ) is executed when the tov0 flag, located in tifr0, is set. bit 76543210 0x23 icr0[15:8] icr0h 0x22 icr0[7:0] icr0l read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 0x2b ? ?icie0 ? ? ocie0b ocie0a toie0 timsk0 read/write r r r/w r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0
79 8127c?avr?10/09 attiny4/5/9/10 11.11.9 tifr0 ? timer/counter interrupt flag register 0 ? bits 7:6, 4:3 ? reserved bits these bits are reserved for future use. for ensuring compatibility with future devices, these bits must be written to zero when the register is written. ? bit 5 ? icf0: timer/count er0, input capture flag this flag is set when a capture event occurs on the icp0 pin. when the input capture register (icr0) is set by the wgm03:0 to be used as the to p value, the icf0 flag is set when the coun- ter reaches the top value. icf0 is automatically cleared when the input capt ure interrupt vector is executed. alternatively, icf0 can be cleared by writing a logic one to its bit location. ? bit 2 ? ocf1b: timer/counter0, output compare b match flag this flag is set in the time r clock cycle after the counter (tcnt0) value matches the output compare register b (ocr0b). note that a forced output compare (0 b) strobe will not set the ocf0b flag. ocf1b is automatically cleared when the outp ut compare match b interrupt vector is exe- cuted. alternatively, ocf1b can be cleared by writing a logic one to its bit location. ? bit 1 ? ocf0a: timer/counter0, output compare a match flag this flag is set in the time r clock cycle after the counter (tcnt0) value matches the output compare register a (ocr0a). note that a forced output compare (1 a) strobe will not set the ocf0a flag. ocf0a is automatically cleare d when the output compare matc h a interrupt vector is exe- cuted. alternatively, ocf0a can be cleared by writing a logic one to its bit location. ? bit 0 ? tov0: timer/counter0, overflow flag the setting of this flag is dependent of the wg m03:0 bits setting. in normal and ctc modes, the tov0 flag is set when the timer overflows. see table 11-5 on page 75 for the tov0 flag behavior when using another wgm03:0 bit setting. tov0 is automatically cleared when the timer/c ounter0 overflow interr upt vector is executed. alternatively, tov0 can be cleared by writing a logic one to its bit location. 11.11.10 gtccr ? general timer/counter control register ? bit 7 ? tsm: timer/counter synchronization mode writing the tsm bit to one activa tes the timer/counter synchroniz ation mode. in this mode, the value that is written to the psr bit is kep t, hence keeping the prescaler reset signal asserted. bit 765432 1 0 0x2a ? ?icf0 ? ? ocf0b ocf0a tov0 tifr0 read/write r r r/w r r r/w r/w r/w initial value000000 0 0 bit 7 6 5 4 3 2 1 0 0x2f tsm ? ? ? ? ? ? psr gtccr read/write r/w r r r r r r r/w initial value 0 0 0 0 0 0 0 0
80 8127c?avr?10/09 attiny4/5/9/10 this ensures that the timer/counter is halted an d can be configured without the risk of advanc- ing during configuration. when the tsm bit is writ ten to zero, the psr bit is cleared by hardware, and the timer/counter start counting. ? bit 0 ? psr: prescaler 0 reset timer/counter 0 when this bit is one, the timer/ counter0 prescaler will be reset. this bit is normally cleared immediately by hardware, except if the tsm bit is set.
81 8127c?avr?10/09 attiny4/5/9/10 12. analog comparator the analog comparator compares the input va lues on the positive pin ain0 and negative pin ain1. when the voltage on the positive pin ain0 is higher than the voltage on the negative pin ain1, the analog comparator output, aco, is se t. the comparator can trigger a separate inter- rupt, exclusive to the analog comparator. the user can select interrupt triggering on comparator output rise, fall or toggle. a block diagram of t he comparator and its surrounding logic is shown in figure 12-1 . figure 12-1. analog comparator block diagram. see figure 1-1 on page 2 for pin use of analog comparator, and table 10-4 on page 49 and table 10-5 on page 50 for alternate pin usage. 12.1 register description 12.1.1 acsr ? analog comparator control and status register ? bit 7 ? acd: analog comparator disable when this bit is written logic one, the power to th e analog comparator is switched off. this bit can be set at any time to turn off the analog comparator, thus reduci ng power consumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be dis- abled by clearing the acie bit in acsr. otherwise an interrupt can occur when the bit is changed. ? bits 6 ? res: reserved bit this bit is reserved and will always read zero. ? bit 5 ? aco: analog comparator output enables output of analog comparator. the output of the analog comparator is synchronized and then directly connected to aco. the synchronization introduces a delay of 1 - 2 clock cycles. bit 76543210 0x1f acd ? aco aci acie acic acis1 acis0 acsr read/write r/w r r r/w r/w r/w r/w r/w initial value00000000
82 8127c?avr?10/09 attiny4/5/9/10 ? bit 4 ? aci: analog comparator interrupt flag this bit is set by hardware when a comparator output event triggers t he interrupt mode defined by acis1 and acis0. the analog comparator interr upt routine is executed if the acie bit is set and the i-bit in sreg is set. aci is cleared by hardware when executing the corresponding inter- rupt handling vector. alternatively, aci is cleared by writing a logic one to the flag. ? bit 3 ? acie: analog comparator interrupt enable when the acie bit is written logic one, the a nalog comparator interrupt request is enabled. when written logic zero, the interrupt request is disabled. ? bit 2 ? acic: analog comparator input capture enable when set, this bit enables the input capture function in timer/counter0 to be triggered by the analog comparator. in this case, the comparator output is directly connected to the input capture front-end logic, using the noise canceler and edge select featur es of the timer/counter0 input capture interrupt. to make the comparator trigger the timer/counter0 input capture interrupt, the icie1 bit in ?timsk0 ? timer/counter in terrupt mask register 0? must be set. when this bit is cleared, no connection between the analog comparator and the input capture function exists. ? bits 1:0 ? acis1, acis0: analog comparator interrupt mode select these bits determine which comparator events that trigger the analog comparator interrupt. the different settings are shown in table 12-1 . when changing the acis1/acis0 bits, the analog comparator interrupt must be disabled by clearing its interrupt enable bit in ?acsr ? an alog comparator control and status register?. otherwise an interrupt can occur when the bits are changed. 12.1.2 didr0 ? digital i nput disable register 0 ? bits 1:0 ? adc1d, adc0d: digital input disable when this bit is set, the digital input buffer on pin ain1 (adc1) / ain0 (adc0) is disabled and the corresponding pin register bit will read as zero. when used as an analog input but not required as a digital input the power consumpti on in the digital input buffer can be reduced by writing this bit to logic one. table 12-1. selecting source for analog comparator interrupt. acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle. 01reserved 1 0 comparator interrupt on falling output edge. 1 1 comparator interrupt on rising output edge. bit 76543210 0x17 ? ? ? ? ? ? adc1d adc0d didr0 read/writerrrrrrr/wr/w initial value00000000
83 8127c?avr?10/09 attiny4/5/9/10 13. analog to digital converter 13.1 features ? 8-bit resolution ? 0.5 lsb integral non-linearity ? 1 lsb absolute accuracy ? 65s conversion time ? 15 ksps at full resolution ? four multiplexed single ended input channels ? input voltage range: 0 ? v cc ? supply voltage range: 2.5v ? 5.5v ? free running or single conversion mode ? adc start conversion by auto tr iggering on interrupt sources ? interrupt on adc conversion complete ? sleep mode noise canceler 13.2 overview attiny5/10 feature an 8-bit, successive approxim ation adc. the adc is connected to a 4-chan- nel analog multiplexer which allows four single-ended voltage inputs constructed from the pins of port b. the single-ended voltage inputs refer to 0v (gnd). the adc contains a sample-and-hol d-circuit, which ensures that the input voltage to the adc is held at a constant level during conversion . a block diagram of the adc is shown in figure 13-1 on page 84 . internal reference voltage of v cc is provided on-chip. the adcis not available in attiny4/9. 13.3 operation in order to be able to use the adc the powe r reduction bit, pradc, in the power reduction register must be disabled. this is done by clearing the pradc bit. see ?prr ? power reduc- tion register? on page 26 for more details. the adc is enabled by setting the adc enable bit, aden in ?adcsra ? adc control and sta- tus register a?. input ch annel selections will not go into effe ct until aden is set. the adc does not consume power when aden is cleared, so it is recommended to switch off the adc before entering power saving sleep modes. the adc converts an analog input voltage to an 8-bit digital value using successive approxima- tion. the minimum value represents gnd and the maximum value represents the voltage on v cc . the analog input channel is select ed by writing mux1:0 bits. see ?admux ? adc multiplexer selection register? on page 93 . any of the adc input pins can be selected as single ended inputs to the adc. the adc generates an 8-bit result which is presented in the adc data register. see ?adcl ? adc data register? on page 95 . the adc has its own interrupt re quest which can be triggered when a conversion completes.
84 8127c?avr?10/09 attiny4/5/9/10 figure 13-1. analog to digital converter block schematic 13.4 starting a conversion make sure the adc is powered by clearing the adc power reduction bit, pradc, in the power reduction register, prr (see ?prr ? power reduction register? on page 26 ). a single conversion is started by writing a l ogical one to the adc start conversion bit, adsc. this bit stays high as long as the conversi on is in progress and will be cleared by hardware when the conversion is completed. if a different data channel is selected while a conversion is in progress, the adc will finish the current conv ersion before performing the channel change. alternatively, a conversion can be triggered auto matically by various sour ces. auto triggering is enabled by setting the adc auto trigger enable bi t, adate in adcsra. the trigger source is selected by setting the adc trigger select bits, adts in ?adcsrb ? adc control and status register b?. see table 13-4 on page 95 for a list of the trigger sources. when a positive edge occurs on the selected trigger signal, the adc pr escaler is reset and a conversion is started. this provides a method of starting conversions at fixed intervals. if the trig ger signal still is set when the conversion completes, a new conversion will not be started. if another positive edge occurs on the trigger signal during conversion, the edge will be ignored. note that an interrupt flag will be set even if the specif ic interrupt is disabled. a conv ersion can thus be triggered with- out causing an interrupt. however, the interrupt flag must be cleared in order to trigger a new conversion at the next interrupt event. admux decoder 8-bit data bus mux1 mux0 adcsra adcl adps0 aden adps1 adps2 adate adif adsc trigger select vref adts2:0 start prescaler adc7:0 conversion logic 8-bit dac adcsrb - + a dc3 a dc2 a dc1 a dc0 v cc input mux interrupt flags adie adc irq sample & hold comparator channel
85 8127c?avr?10/09 attiny4/5/9/10 figure 13-2. adc auto trigger logic using the adc interrupt flag as a trigger source makes the adc start a new conversion as soon as the ongoing conversion has finished. the adc then operates in free running mode, con- stantly sampling and updating the adc data register. the first conversion must be started by writing a logical one to bit adsc bit in adcs ra. in this mode the adc will perform successive conversions independently of whether the a dc interrupt flag, adif is cleared or not. if auto triggering is enabled, single conversi ons can be started by writing adsc in adcsra to one. adsc can also be used to determine if a conversion is in progress. the adsc bit will be read as one during a conversion, independe ntly of how the conversion was started. 13.5 prescaling and conversion timing by default, the successive approximation circuitry requires an input clock frequency between 50 khz and 200 khz to get maximum resolution. figure 13-3. adc prescaler the adc module contains a pr escaler, as illustrated in figure 13-3 on page 85 , which generates an acceptable adc clock frequency from any cpu frequency above 100 khz. the prescaling is set by the adps bits in adcsra. the prescaler starts counting from the moment the adc is adsc adif source 1 source n adts[2:0] conversion logic prescaler start clk adc . . . . edge detector adate 7-bit adc prescaler adc clock source ck adps0 adps1 adps2 ck/128 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 reset aden start
86 8127c?avr?10/09 attiny4/5/9/10 switched on by setting the aden bit in adcsra. the prescaler keeps running for as long as the aden bit is set, and is continuously reset when aden is low. when initiating a single ended conversion by se tting the adsc bit in adcsra, the conversion starts at the following rising edge of the adc clock cycle. a normal conversion takes 13 adc clock cycles, as summarised in table 13-1 on page 87 . the first conversion after the adc is switched on (aden in adcsra is set) takes 25 adc clock cycles in order to initialize the analog circuitry. see figure 13-4 . figure 13-4. adc timing diagram, first conver sion (single conversion mode) the actual sample-and-hold takes place 3 adc cl ock cycles after the start of a normal conver- sion and 16 adc clock cycles after the start of a first conversion. see figure 13-5 . when a conversion is complete, the result is written to the adc data regi sters, and adif is set. in sin- gle conversion mode, adsc is cleared simultan eously. the software may then set adsc again, and a new conversion will be initiated on the first rising adc clock edge. figure 13-5. adc timing diagram, single conversion when auto triggering is used, the prescale r is reset when the trig ger event occurs. see figure 13-6 . this assures a fixed delay from the trigger event to the start of conversion. in this mode, the sample-and-hold takes place two adc clo ck cycles after the rising edge on the trigger source signal. three additional cpu clock cycles are used for synchronization logic. conversion result adc clock adsc sample & hold adif adcl cycle number aden 1 212 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 first conversion next conversion 3 mux update mux update conversion complete 1 2 3 4 5 6 7 8 9 10 11 12 13 conversion result adc clock adsc adif adcl cycle number 12 one conversion next conversion 3 sample & hold mux update conversion complete mux update
87 8127c?avr?10/09 attiny4/5/9/10 figure 13-6. adc timing diagram, auto triggered conversion in free running mode (see figure 13-7 ), a new conversion will be st arted immediately after the conversion completes, wh ile adsc remains high. figure 13-7. adc timing diagram, free running conversion for a summary of conversion times, see table 13-1 . table 13-1. adc conversion time condition sample & hold (cycles from start of conversion) conversion time (cycles) first conversion 16.5 25 normal conversions 3.5 13 auto triggered conversions 4 13.5 1 2 3 4 5 6 7 8 9 10 11 12 13 conversion result a dc clock t rigger s ource a dif a dcl c ycle number 12 one conversion next conversion conversion complete prescaler reset a date prescaler reset sample & hold mux update 11 12 13 conversion result adc clock adsc adif adcl cycle number 12 one conversion next conversion 34 conversion complete sample & hold mux update
88 8127c?avr?10/09 attiny4/5/9/10 13.6 changing channel the muxn bits in the admux register are single buffered through a temporary register to which the cpu has random access. this ensures that the channel selection only takes place at a safe point during the conversion. the channel is continuously updated until a conversion is started. once the conversion starts, the channel selecti on is locked to ensure a sufficient sampling time for the adc. continuous updating resumes in the last adc clock cycle before the conversion completes (adif in adcsra is set). note that the conversion starts on the following rising adc clock edge after adsc is written. the user is thus advised not to write new channel selection values to admux until one adc clock cycle after adsc is written. if auto triggering is used, the exact time of t he triggering event can be indeterministic. special care must be taken when updating the admux register, in order to control which conversion will be affected by the new settings. if both adate and aden is writ ten to one, an interrupt event can occur at any time. if the admux register is changed in this period, the user cannot tell if the next conversion is based on the old or the new settings. admux can be safely updated in the following ways: ? when adate or aden is cleared. ? during conversion, minimum one adc clock cycle after the trigger event. ? after a conversion, before the interrupt flag used as trigger source is cleared. when updating admux in one of these conditions, the new settings will affect the next adc conversion. 13.6.1 adc input channels when changing channel selections, the user should observe the following guidelines to ensure that the correct channel is selected: ? in single conversion mode, always select the channel before starting the conversion. the channel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the conversion to complete before changing the channel selection. ? in free running mode, always select the chan nel before starting the first conversion. the channel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the first conversion to complete, and then change the channel selection. since the next conversion has already started automatically, the next result will reflect the previous channel selectio n. subsequent conversi ons will reflect the new channel selection. 13.6.2 adc voltage reference the reference voltage of the adc determines the conversion range, which in this case is limited to 0v (v gnd ) and v ref = v cc . channels that exceed v ref will result in code s saturated at 0xff. 13.7 adc noise canceler the adc features a noise canceler that enables conversion during sleep mode to reduce noise induced from the cpu core and other i/o peripher als. the noise canceler can be used with adc noise reduction and idle mode. to make use of this feature, the following procedure should be used:
89 8127c?avr?10/09 attiny4/5/9/10 ? make sure that the adc is enabled and is not busy converting. single conversion mode must be selected and the adc conversion complete interrupt must be enabled. ? enter adc noise reduction mode (or idle mode ). the adc will start a conversion once the cpu has been halted. ? if no other interrupts occur before the adc conversion completes, the adc interrupt will wake up the cpu and execute the adc conversi on complete interrupt routine. if another interrupt wakes up the cpu before the adc co nversion is complete , that interrupt will be executed, and an adc conversi on complete interrup t request will be generated when the adc conversion completes. the cpu will remain in active mode un til a new sleep command is executed. note that the adc will no t be automatically turned off when entering other sleep modes than idle mode and adc noise reduction mode. the user is advised to write zero to aden before enter- ing such sleep modes to avoid excessive power consumption. 13.8 analog input circuitry the analog input circuitry for sing le ended channels is illustrated in figure 13-8 an analog source applied to adcn is subjected to the pin capacitance and input leak age of that pin, regard- less of whether that channel is selected as input for the adc. when the channel is selected, the source must drive the s/h (sample and hold) ca pacitor through the series resistance (combined resistance in the input path). figure 13-8. analog input circuitry the capacitor in figure 13-8 depicts the total capacitance, including the sample/hold capacitor and any stray or parasitic capacitance inside the device. the value given is worst case. the adc is optimized for analog signals with an output impedance of approximately 10 k , or less. with such sources, the sampling time will be negligible. if a sour ce with higher impedance is used, the sampling time will depend on how l ong time the source needs to charge the s/h capacitor. this can vary widely. the user is recommended to only use low impedance sources with slowly varying signals, since this minimizes the required charge transfer to the s/h capacitor. signal components higher than the nyquist frequency (f adc /2) should not be present to avoid distortion from unpredictable signal convolution. the user is advised to remove high frequency components with a low-pass filter before appl ying the signals as inputs to the adc. adcn i ih 1..100 k ohm c s/h = 14 pf v cc /2 i il
90 8127c?avr?10/09 attiny4/5/9/10 13.9 noise canceling techniques digital circuitry inside and outside the device ge nerates emi which might affect the accuracy of analog measurements. when conversion accuracy is critical, the noise level can be reduced by applying the following techniques: ? keep analog signal paths as short as possible. ? make sure analog tracks run over the analog ground plane. ? keep analog tracks well away from high-speed switching digital tracks. ? if any port pin is used as a digital output, it mustn?t switch while a conversion is in progress. ? place bypass capacitors as close to v cc and gnd pins as possible. where high adc accuracy is required it is recommended to use adc noise reduction mode, as described in section 13.7 on page 88 . a good system design with properly placed, external bypass capacitors does reduce the need for using adc noise reduction mode 13.10 adc accuracy definitions an n-bit single-ended adc converts a voltage linearly between gnd and v ref in 2 n steps (lsbs). the lowest code is read as 0, and the highest code is read as 2 n -1. several parameters describe the deviation from the ideal behavior: ? offset: the deviation of the first transition (0 x00 to 0x01) compared to the ideal transition (at 0.5 lsb). ideal value: 0 lsb. figure 13-9. offset error output code v ref input voltage ideal adc actual adc offset error
91 8127c?avr?10/09 attiny4/5/9/10 ? gain error: after adjusting for offset, the gain error is found as the deviation of the last transition (0xfe to 0xff) compared to the ideal transition (at 1.5 lsb below maximum). ideal value: 0 lsb figure 13-10. gain error ? integral non-linearity (inl): afte r adjusting for offset and gain error, the inl is the maximum deviation of an actual transition compared to an ideal transition for any code. ideal value: 0 lsb. figure 13-11. integral non-linearity (inl) output code v ref input voltage ideal adc actual adc gain error output code v ref input voltage ideal adc actual adc inl
92 8127c?avr?10/09 attiny4/5/9/10 ? differential non-linearity (dnl): the maximum deviation of the actu al code width (the interval between two adjacent transitions) from the ideal code width (1 lsb). ideal value: 0 lsb. figure 13-12. differential non-linearity (dnl) ? quantization error: due to the quantization of th e input voltage into a finite number of codes, a range of input volt ages (1 lsb wide) will code to the same value. always 0.5 lsb. ? absolute accuracy: the maximum deviation of an actual (unadjusted) transition compared to an ideal transition for any code. th is is the compound effect of offset, gain error, differential error, non-linearity, and quantization error. ideal value: 0.5 lsb. 13.11 adc conversion result after the conversion is complete (adif is high ), the conversion result can be found in the adc data register (adcl). for single ended conversion, the result is where v in (see table 13-2 on page 93 ) is the voltage on the selected input pin and v cc is the voltage reference. 0x00 repres ents analog ground, and 0xff represents the selected reference voltage minus one lsb. output code 0xff 0x00 0 v ref input voltage dnl 1 lsb adcl v in 256 ? v cc ----------------------- =
93 8127c?avr?10/09 attiny4/5/9/10 13.12 register description 13.12.1 admux ? adc multiplexer se lection register ? bits 7:2 ? res: reserved bits these bits are reserved and will always read zero. ? bits 1:0 ? mux1:0: analog channel selection bits the value of these bits selects which combinatio n of analog inputs are connected to the adc. see table 13-2 for details. if these bits are changed during a conversion, the change will not go in effect until the conversion is complete (adif in adcsra is set). 13.12.2 adcsra ? adc control and status register a ? bit 7 ? aden: adc enable writing this bit to one enables the adc. by writi ng it to zero, the adc is turned off. turning the adc off while a conversion is in prog ress, will terminate this conversion. ? bit 6 ? adsc: adc start conversion in single conversion mode, write this bit to one to start each conversion. in free running mode, write this bit to one to start the first conversion. the first conversion after adsc has been written after the adc has been enabled, or if adsc is written at the same time as the adc is enabled, will take 25 adc clock cycles instead of the norma l 13. this first conversi on performs initializa- tion of the adc. adsc will read as one as long as a conversion is in progress. when the co nversion is complete, it returns to zero. writing zero to this bit has no effect. ? bit 5 ? adate: adc auto trigger enable when this bit is written to on e, auto triggering of the adc is enabled. the adc will start a con- version on a positive edge of the selected trigger signal. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb. bit 76543210 0x1b ? ? ? ? ? ? mux1 mux0 admux read/writerrrrrrr/wr/w initial value00000000 table 13-2. input channel selections mux1 mux0 single ended input 0 0 adc0 (pb0) 1 adc1 (pb1) 1 0 adc2 (pb2) 1 adc3 (pb3) bit 76543210 0x1d aden adsc adate adif adie adps2 adps1 adps0 adcsra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
94 8127c?avr?10/09 attiny4/5/9/10 ? bit 4 ? adif: adc interrupt flag this bit is set when an adc conversion complete s and the data registers are updated. the adc conversion complete interrupt is requested if the adie bit is set. adif is cleared by hardware when executing the corresponding interrupt hand ling vector. alternatively, adif is cleared by writing a logical one to the flag. ? bit 3 ? adie: adc interrupt enable when this bit is written to one, the adc c onversion complete interrupt request is enabled. ? bits 2:0 ? adps2:0: adc prescaler select bits these bits determine the division factor betwee n the system clock frequency and the input clock to the adc. 13.12.3 adcsrb ? adc control and status register b ? bits 7:3 ? res: reserved bits these bits are reserved and will always read zero. ? bits 2:0 ? adts2:0: adc auto trigger source if adate in adcsra is written to one, the value of these bits selects which source will trigger an adc conversion. if adate is cleared, the adts2:0 settings will have no effect. a conversion will be triggered by the risi ng edge of the selected interrupt flag . note that switch ing from a trig- ger source that is cleared to a trigger source that is set, will generate a positive edge on the table 13-3. adc prescaler selections adps2 adps1 adps0 division factor 000 2 001 2 010 4 011 8 100 16 101 32 110 64 111 128 bit 76543210 0x1c ? ? ? ? ? adts2 adts1 adts0 adcsrb read/write r r r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0
95 8127c?avr?10/09 attiny4/5/9/10 trigger signal. if aden in adcsra is set, this will start a conversion. switching to free running mode (adts[2:0]=0) will not cause a trigger event, even if t he adc interrupt flag is set . 13.12.4 adcl ? adc data register when an adc conversion is complete, the result is found in the adc register. ? bits 7:0 ? adc7:0: adc conversion result these bits represent the result from the conversion. 13.12.5 didr0 ? digital input disable register 0 ? bits 7:4 ? res: reserved bit these bits are reserved and will always read zero. ? bits 3:0 ? adc3d..adc0d: ad c3..0 digital input disable when this bit is written logic one, the digita l input buffer on the corresponding adc pin is dis- abled. the corresponding pin register bit will always read as zero when this bit is set. when an analog signal is applied to the adc3..0 pin and th e digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer. table 13-4. adc auto trigger source selections adts2 adts1 adts0 trigger source 0 0 0 free running mode 0 0 1 analog comparator 0 1 0 external interrupt flag 0 0 1 1 timer/counter 0 compare match a 1 0 0 timer/counter 0 overflow 1 0 1 timer/counter 0 compare match b 1 1 0 pin change interrupt 0 request 1 1 1 timer/counter 0 capture event bit 76543210 0x19 adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 adcl read/writerrrrrrrr initial value00000000 bit 765432 1 0 0x17 ? ? ? ? adc3d adc2d adc1d adc0d didr0 read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
96 8127c?avr?10/09 attiny4/5/9/10 14. programming interface 14.1 features ? physical layer: ? synchronous data transfer ? bi-directional, half-duplex receiver and transmitter ? fixed frame format with one start bit, 8 data bits, one parity bit and 2 stop bits ? parity error detection, frame error detection and break character detection ? parity generation and collision detection ? automatic guard time insertion between data reception and transmission ? access layer: ? communication based on messages ? automatic exception handling mechanism ? compact instruction set ? nvm programming access control ? tiny programming interface control and status space access control ? data space access control 14.2 overview the tiny programming interface (t pi) supports external programmi ng of all non- volatile memo- ries (nvm). memory programming is done via the nvm controller, by executing nvm controller commands as described in ?memory programming? on page 107 . the tiny programming interface (tpi) provides a ccess to the programming facilities. the inter- face consists of two layers: th e access layer and the physical la yer. the layers are illustrated in figure 14-1 . figure 14-1. the tiny programming interface and related internal interfaces programming is done via the phys ical interface. this is a 3-pi n interface, whic h uses the reset pin as enable, the tpiclk pin as the clock inpu t, and the tpidata pin as data input and output. nvm can be programmed at 5v, only. 14.3 physical layer of tiny programming interface the tpi physical layer handles th e basic low-level serial communication. the tpi physical layer uses a bi-directional, half-duplex serial receiv er and transmitter. the physical layer includes serial-to-parallel and parallel-to-serial data conv ersion, start-of-frame detection, frame error detection, parity error detection, pa rity generation and collision detection. access layer physical layer nvm controller non-volatile memories tpiclk reset t pidata tiny programming interface (tpi) data bus
97 8127c?avr?10/09 attiny4/5/9/10 the tpi is accessed via three pins, as follows: reset : tiny programming interface enable input tpiclk: tiny programming interface clock input tpidata: tiny programming interface data input/output in addition, the v cc and gnd pins must be connected betwe en the external programmer and the device. see figure 14-2 . figure 14-2. using an external programmer for in-system programming via tpi nvm can be programmed at 5v, only. in some designs it may be necessary to protect compo- nents that can not tolerate 5v with, for example, series resistors. 14.3.1 enabling the following sequence enables t he tiny programming interface (see figure 14-3 for guidance): ? apply 5v between v cc and gnd ? depending on the method of reset to be used: ? either: wait t tout (see table 16-4 on page 119 ) and then set the reset pin low. this will reset the device and enable the tpi ph ysical layer. the reset pin must then be kept low for the en tire programming session ? or: if the rstdisbl configuration bi t has been programmed, apply 12v to the reset pin. the reset pin must be kept at 12v for the entire programming session ?wait t rst (see table 16-4 on page 119 ) ? keep the tpidata pin high for 16 tpiclk cycles figure 14-3. sequence for enabling the tiny programming interface attiny4/5/9/10 tpidata/pb0 gnd tpiclk/pb1 pb3/reset v cc pb2 tpi conn application +5v reset t rst tpidata tpiclk 16 x tpiclk cycles
98 8127c?avr?10/09 attiny4/5/9/10 14.3.2 disabling provided that the nvm enable bit has been clear ed, the tpi is automatically disabled if the reset pin is released to inactive high state or, alternatively, if v hv is no longer applied to the reset pin. if the nvm enable bit is not cleared a power do wn is required to exit tpi programming mode. see nvmen bit in ?tpisr ? tiny programming interface status register? on page 106 . 14.3.3 frame format the tpi physical layer supports a fixed frame fo rmat. a frame consists of one character, eight bits in length, and one start bit, a parity bit and two stop bits. data is transferred with the least significant bit first. figure 14-4. serial frame format. symbols used in figure 14-4 : st: start bit (always low) d0-d7: data bits (least significant bit sent first) p: parity bit (using even parity) sp1: stop bit 1 (always high) sp2: stop bit 2 (always high) 14.3.4 parity bit calculation the parity bit is always calculat ed using even parity. the value of the bit is calculated by doing an exclusive-or of all the data bits, as follows: p = d0 ? d1 ? d2 ? d3 ? d4 ? d5 ? d6 ? d7 ? 0 where: p: parity bit using even parity d0-d7: data bits of the character 14.3.5 supported characters the break character is equa l to a 12 bit long low level. it c an be extended be yond a bit-length of 12. figure 14-5. supported characters. tpidata tpiclk sp1 st sp2 idle/st idle p d1 d0 d7 idle/st idle break character data character sp1 st sp2 idle/st idle p d1 d0 d7 tpidata tpidata
99 8127c?avr?10/09 attiny4/5/9/10 14.3.6 operation the tpi physical layer operates synchronously on the tpiclk provided by the external pro- grammer. the dependency between the clock edges and data sampling or data change is shown in figure 14-6 . data is changed at falling edge s and sampled at rising edges. figure 14-6. data changing and data sampling. the tpi physical layer supports two modes of opera tion: transmit and receive. by default, the layer is in receive mode, waiting for a start bi t. the mode of operation is controlled by the access layer. 14.3.7 serial da ta reception when the tpi physical layer is in receive mode, dat a reception is started as soon as a start bit has been detected. each bit that follows the start bit will be samp led at the rising edge of the tpiclk and shifted into the shift register until the second stop bit has been received. when the complete frame is present in the shift register the rece ived data will be available for the tpi access layer. there are three possible exceptions in the receive mode: frame error, parity error and break detection. all these exceptions are signalized to the tpi access layer, which then enters the error state and puts the tpi physical layer in to receive mode, waiting for a break character. ? frame error exception. the frame error except ion indicates the state of the stop bit. the frame error exception is set if the stop bit was read as zero. ? parity error exception. the parity of the data bits is calculated during the frame reception. after the frame is received completely, the result is compared with the parity bit of the frame. if the comparison fails the pari ty error exception is signalized. ? break detection exception. the break detectio n exception is given when a complete frame of all zeros has been received. 14.3.8 serial data transmission when the tpi physical layer is ready to send a ne w frame it initiates data transmission by load- ing the shift register with the data to be transmitted. when the shift register has been loaded with new data, the transmitter shifts one complete frame out on the tpidata line at the transfer rate given by tpiclk. if a collision is detected during tr ansmission, the output driver is disabled. the tpi access layer enters the error state and the tpi physical layer is put into receive mode, waiting for a break character. tpidata tpiclk sample setup
100 8127c?avr?10/09 attiny4/5/9/10 14.3.9 collision de tection exception the tpi physical layer uses one bi-directional data line for both data reception and transmission. a possible drive contention may occur, if the external programmer and the tpi physical layer drive the tpidata line simultaneously. in order to reduce the effect of the drive contention, a collision detection mechanism is supported. the collision detecti on is based on the way the tpi physical layer drives the tpidata line. the tpidata line is driven by a tri-state, push-p ull driver with internal pull-up. the output driver is always enabled when a logical zero is sent. when sending successive logical ones, the output is only driven actively during the first clock cycle. after this, the output driver is automatically tri- stated and the tpidata line is kept high by the internal pull-up. the output is re-enabled, when the next logical zero is sent. the collision detection is enabled in transmit mode, when the output driver has been disabled. the data line should now be kept high by the internal pull-up and it is monitored to see, if it is driven low by the ex ternal programmer. if the output is read low, a co llision has been detected. there are some potential pit-falls related to the way collision dete ction is performed. for exam- ple, collisions cannot be detecte d when the tpi physical laye r transmits a bit-stream of successive logical zeros, or bit-st ream of alternating logical ones and zeros. this is because the output driver is active all the time, preventing polling of the tpidata line. however, within a sin- gle frame the two stop bits should always be transmitted as logical ones, enabling collision detection at least once per frame (as long as the frame format is not violated regarding the stop bits). the tpi physical layer will cease transmission w hen it detects a collision on the tpidata line. the collision is signalized to the tpi access la yer, which immediately ch anges the physical layer to receive mode and goes to the error state. the tpi access layer can be recovered from the error state only by sending a break character. 14.3.10 direction change in order to ensure correct timing of the hal f-duplex operation, a simple guard time mechanism has been added to the physical layer. when t he tpi physical layer changes from receive to transmit mode, a configurable num ber of additional idle bits are inserted before the start bit is transmitted. the minimum transition time betwe en receive and transmit mode is two idle bits. the total idle time is the specifie d guard time plus two idle bits. the guard time is configured by dedicated bits in the tpipcr register. the default guard time value after the physical layer is initialized is 128 bits. the external programmer looses control of the tpidata line when the tpi target changes from receive mode to transmit. the guard time feature relaxes this critical phase of the communica- tion. when the external programmer changes from receive mode to transmit, a minimum of one idle bit should be inserted before the start bit is transmitted. 14.4 access layer of tiny programming interface the tpi access layer is respons ible for handling the communication with the external program- mer. the communication is based on message format, where each message comprises an instruction followed by one or more byte-sized operands. the instruction is always sent by the external programmer but operands are sent either by the external programmer or by the tpi access layer, depending on the type of instruction issued.
101 8127c?avr?10/09 attiny4/5/9/10 the tpi access layer controls the character transf er direction on the tpi physical layer. it also handles the recovery from the error state after exception. the control and status space (css ) of the tiny programming inte rface is allocated for control and status registers in the tpi access layer. the css consist of registers directly involved in the operation of the tpi itself. these register are accessible using the sldcs and sstcs instructions. the access layer can also access the data space, either directly or indirectly using the pointer register (pr) as the address pointer. the data space is accessible usi ng the sld, sst, sin and sout instructions. the address pointer can be stored in the pointer register using the sldpr instruction. 14.4.1 message format each message comprises an instruction followed by one or more byte operands. the instruction is always sent by the external programmer. depending on the instruction all the following oper- ands are sent either by the external programmer or by the tpi. the messages can be categorized in two ty pes based on the instruction, as follows: ? write messages. a write message is a reques t to write data. the write message is sent entirely by the external programmer. th is message type is used with the sstcs, sst, stpr, sout and skey instructions. ? read messages. a read message is a request to read data. the tpi reacts to the request by sending the byte operands. this message type is used with the sldcs, sld and sin instructions. all the instructions except the skey instruction require the instru ction to be followed by one byte operand. the skey instruction requires 8 byte operands. for more information, see the tpi instruction set on page 102 . 14.4.2 exception handling and synchronisation several situations are consi dered exceptions from normal oper ation of the tpi. when the tpi physical layer is in receive mode, these exceptions are: ? the tpi physical layer detects a parity error. ? the tpi physical layer detects a frame error. ? the tpi physical layer recognizes a break character. when the tpi physical layer is in transmit mode, the possible exceptions are: ? the tpi physical layer detects a data collision. all these exceptions are signalized to the tpi access layer. the access layer responds to an exception by aborting any on-going operation and enters the error state. the access layer will stay in the error state until a break character has been received, after whic h it is taken back to its default state. as a consequence, the extern al programmer can always synchronize the proto- col by simply transmitting two successive break characters.
102 8127c?avr?10/09 attiny4/5/9/10 14.5 instruction set the tpi has a compact instruction set that is us ed to access the tpi control and status space (css) and the data space. the in structions allow the external programmer to access the tpi, the nvm controller and the nvm me mories. all instructions exce pt skey require one byte oper- and following the inst ruction. the skey instructio n is followed by 8 data bytes. all instructions are byte-sized. the tpi instruction se t is summarised in table 14-1 . 14.5.1 sld - serial load from data space using indirect addressing the sld instruction uses indirect addressing to load data from the data space to the tpi physi- cal layer shift-register for serial read-out. the data space location is pointed by the pointer register (pr), where the address must have be en stored before data is accessed. the pointer register is either left unchanged by the oper ation, or post-incremented, as shown in table 14-2 . table 14-1. instruction set summary mnemonic operand description operation sld data, pr serial load from data space using indirect addressing data ds[pr] sld data, pr+ serial load from data space using indirect addressing and post-increment data ds[pr] pr pr+1 sst pr, data serial store to data space using indirect addressing ds[pr] data sst pr+, data serial store to data space using indirect addressing and post-increment ds[pr] data pr pr+1 sstpr pr, a serial store to pointer register using direct addressing pr[a] data sin data, a serial in from data space data i/o[a] sout a, data serial out to data space i/o[a] data sldcs data, a serial load from control and status space using direct addressing data css[a] sstcs a, data serial store to control and status space using direct addressing css[a] data skey key, {8{data}} serial key key {8{data}} table 14-2. the serial load from data space (sld) instruction operation opcode remarks register data ds[pr] 0010 0000 pr pr unchanged data ds[pr] 0010 0100 pr pr + 1 post increment
103 8127c?avr?10/09 attiny4/5/9/10 14.5.2 sst - serial store to data space using indirect addressing the sst instruction uses indirect addressing to stor e into data space the byte that is shifted into the physical layer shift register. the data space lo cation is pointed by the pointer register (pr), where the address must have been stored befo re the operation. the pointer register can be either left unchanged by the operation, or it can be post-incre mented, as shown in table 14-3 . 14.5.3 sstpr - serial store to pointer register the sstpr instruction stores the data byte that is shifted into the physica l layer shift register to the pointer register (pr). the address bit of the instruction specifies which byte of the pointer register is accessed, as shown in table 14-4 . 14.5.4 sin - serial in from i/o space using direct addressing the sin instruction loads data byte from the i/o s pace to the shift register of the physical layer for serial read-out. the instuction uses dire ct addressing, the address consisting of the 6 address bits of the inst ruction, as shown in table 14-5 . 14.5.5 sout - serial out to i/o space using direct addressing the sout instruction stores the data byte that is shifted into the physical layer shift register to the i/o space. the instruction uses direct addressing , the address consisting of the 6 address bits of the instruction, as shown in table 14-6 . table 14-3. the serial store to data space (sld) instruction operation opcode remarks register ds[pr] data 0110 0000 pr pr unchanged ds[pr] data 0110 0100 pr pr + 1 post increment table 14-4. the serial store to pointer register (sstpr) instruction operation opcode remarks pr[a] data 0110 100a bit ?a? addresses pointer register byte table 14-5. the serial in from i/o space (sin) instruction operation opcode remarks data i/o[a] 0aa1 aaaa bits marked ?a? form the direct, 6-bit addres table 14-6. the serial out to i/o space (sout) instruction operation opcode remarks i/o[a] data 1aa1 aaaa bits marked ?a? form the direct, 6-bit addres
104 8127c?avr?10/09 attiny4/5/9/10 14.5.6 sldcs - serial load data from control and status space using direct addressing the sldcs instruction loads data byte from the tpi control and status space to the tpi physi- cal layer shift register for serial read-out. the sldcs instruction uses direct addressing, the direct address consisting of the 4 address bits of the instruction, as shown in table 14-7 . 14.5.7 sstcs - serial store data to control and status space using direct addressing the sstcs instruction stores the data byte that is shifted into the tpi physical layer shift regis- ter to the tpi control and status space. the sstcs instruction uses direct addressing, the direct address consisting of the 4 address bits of the instruction, as shown in table 14-8 . 14.5.8 skey - serial key signaling the skey instruction is used to signal the activation key that enables nvm programming. the skey instruction is followed by the 8 data bytes that includes the activation key, as shown in table 14-9 . 14.6 accessing the non-volati le memory controller by default, nvm programming is not enabled. in order to access the nvm controller and be able to program the non-volatile memories, a unique ke y must be sent using the skey instruction. the 64-bit key that will enable nv m programming is given in table 14-10 . after the key has been given, the non-volatile memory enable (nvmen) bit in the tpi status register (tpisr) must be polled until the non-volatile memory has been enabled. nvm programming is disabled by writing a logical zero to the nvmen bit in tpisr. table 14-7. the serial load data from control and status space (sldcs) instruction operation opcode remarks data css[a] 1000 aaaa bits marked ?a? form the direct, 4-bit addres table 14-8. the serial store data to control and status space (sstcs) instruction operation opcode remarks css[a] data 1100 aaaa bits marked ?a? form the direct, 4-bit addres table 14-9. the serial key signalin g (skey) instruction operation opcode remarks key {8[data}} 1110 0000 data bytes follow after instruction table 14-10. enable key for non-volatile memory programming key value nvm program enable 0x1289ab45cdd888ff
105 8127c?avr?10/09 attiny4/5/9/10 14.7 control and status sp ace register descriptions the control and status registers of the tiny programming interface are mapped in the control and status space (css) of the interface. these registers are not part of the i/o register map and are accessible via sldcs and sstcs instruct ions, only. the control and status registers are directly involved in configurat ion and status monitoring of the tpi. a summary of css registers is shown in table 14-11 . 14.7.1 tpiir ? tiny programming in terface identification register ? bits 7:0 ? tpiic: tiny programming interface identification code these bits give the identification code for the tiny programming interface. the code can be used be the external programmer to identify t he tpi. the identification code of the tiny pro- gramming interface is shown in table 14-12 .. 14.7.2 tpipcr ? tiny programming interface physical layer control register ? bits 7:3 ? res: reserved bits these bits are reserved and will always read zero. table 14-11. summary of control and status registers addr. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 0x0f tpiir tiny programming interface identification code 0x0e ... 0x03 reserved ? ? ? ? ? ? ? ? 0x02 tpipcr ? ? ? ? ? gt2 gt1 gt0 0x01 reserved ? ? ? ? ? ? ? ? 0x00 tpisr ? ? ? ? ? ? nvmen ? bit 76543210 css: 0x0f programming interface identification code tpiir read/writerrrrrrrr initial value00000000 table 14-12. identification code for tiny programming interface code value interface identification 0x80 bit 76543210 css: 0x02 ? ? ? ? ? gt2 gt1 gt0 tpipcr read/writerrrrrr/wr/wr/w initial value00000000
106 8127c?avr?10/09 attiny4/5/9/10 ? bits 2:0 ? gt[2:0]: guard time these bits specify the number of additional idle bits that are inserted to the idle time when changing from reception mode to transmission mode. additional delays are not inserted when changing from transmission mode to reception. the total idle time when changing from reception to transmission mode is guard time plus two idle bits. table 14-13 shows the available guard time settings. the default guard time is 128 idle bits. to speed up the communication, the guard time should be set to the shortest safe value. 14.7.3 tpisr ? tiny programming interface status register ? bits 7:2, 0 ? res: reserved bits these bits are reserved and will always read zero. ? bit 1 ? nvmen: non-volatile memory programming enabled nvm programming is enabled when this bit is set. the external programmer can poll this bit to verify the interface has been successfully enabled. nvm programming is disabled by writing this bit to zero. table 14-13. guard time settings gt2 gt1 gt0 guard time (number of idle bits) 0 0 0 +128 (default value) 001+64 010+32 011+16 100+8 101+4 110+2 111+0 bit 76543210 css: 0x00 ? ? ? ? ? ?nvmen ?tpipcr read/writerrrrrrr/wr initial value00000000
107 8127c?avr?10/09 attiny4/5/9/10 15. memory programming 15.1 features ? two embedded non-volatile memories: ? non-volatile memory lock bits (nvm lock bits) ? flash memory ? four separate sections inside flash memory: ? code section (program memory) ? signature section ? configuration section ? calibration section ? read access to all non-volatile me mories from application software ? read and write access to non-volatile memories from external programmer: ? read access to all non-volatile memories ? write access to nvm lock bits, flash code section and flash configuration section ? external programming: ? support for in-system and mass production programming ? programming through the tiny programming interface (tpi) ? high security wi th nvm lock bits 15.2 overview the non-volatile memory (nvm) controller manages all access to the non-volatile memories. the nvm controller controls all nvm timing and access privileges, and holds the status of the nvm. during normal execution the cpu will execute code from the code section of the flash memory (program memory). when entering sleep and no pr ogramming operations are active, the flash memory is disabled to minimize power consumption. all nvm are mapped to the data memory. application software can read the nvm from the mapped locations of data memory using lo ad instruction with indirect addressing. the nvm has only one read port and, therefore, the next instruction and the data can not be read simultaneously. when the application reads data from nvm locations mapped to the data space, the data is read first before the next instruction is fetched. the cpu execution is here delayed by one system clock cycle. internal programming operations to nvm have been disabled and the nvm therefore appears to the application software as read-only. internal write or erase operations of the nvm will not be successful. the method used by the external programmer for wr iting the non-volatile memories is referred to as external pr ogramming. external progra mming can be done both in -system or in mass pro- duction. see figure 14-2 on page 97 . the external programmer can read and program the nvm via the tiny programming interface (tpi). in the external programming mode all nvm can be read and programmed, except the signature and the calibration sections which are read-only. nvm can be programmed at 5v, only.
108 8127c?avr?10/09 attiny4/5/9/10 15.3 non-volatile memories the attiny4/5/9/10 have the following, embedded nvm: ? non-volatile memory lock bits ? flash memory with four separate sections 15.3.1 non-volatile memory lock bits the attiny4/5/9/10 provide tw o lock bits, as shown in table 15-1 . the lock bits can be left unprogrammed ("1") or can be programmed ("0") to obtain the addi- tional security shown in table 15-2 . lock bits can be erased to "1" with the chip erase command, only. notes: 1. program the configuration section bits before programming nvlb1 and nvlb2. 2. "1" means unprogrammed, "0" means programmed table 15-1. lock bit byte lock bit bit no description default value 7 1 (unprogrammed) 6 1 (unprogrammed) 5 1 (unprogrammed) 4 1 (unprogrammed) 3 1 (unprogrammed) 2 1 (unprogrammed) nvlb2 1 non-volatile lock bit 1 (unprogrammed) nvlb1 0 non-volatile lock bit 1 (unprogrammed) table 15-2. lock bit protection modes memory lock bits (1) protection type lock mode nvlb2 (2) nvlb1 (2) 1 1 1 no memory lock feature enabled 210 further programming of the flash memory is disabled in the external programming mode. the configuration section bits are locked in the external programming mode 300 further programming and verification of the flash is disabled in the external programming mode. the configuration section bits are locked in the external programming mode
109 8127c?avr?10/09 attiny4/5/9/10 15.3.2 flash memory the embedded flash memory of attiny4/5/9/10 has four separate sections, as shown in table 15-3 and table 15-3 . notes: 1. this section is read-only. notes: 1. this section is read-only. 15.3.3 configuration section attiny4/5/9/10 have one configurat ion byte, which resides in the configuration section. see table 15-5 . table 15-6 briefly describes the functionality of all configuration bits and how they are mapped into the configuration byte. table 15-3. number of words and pages in the flash (attiny9/10) section size (bytes) page size (words) pages waddr paddr code (program memory) 1024 8 64 [3:1] [9:4] configuration 8 8 1 [3:1] ? signature (1) 16 8 2 [3:1] [4:4] calibration (1) 8 8 1 [3:1] ? table 15-4. number of words and pages in the flash (attiny4/5) section size (bytes) page size (words) pages waddr paddr code (program memory) 512 8 32 [3:1] [9:4] configuration 8 8 1 [3:1] ? signature (1) 16 8 2 [3:1] [4:4] calibration (1) 8 8 1 [3:1] ? table 15-5. configuration bytes configuration word address configuration word data high byte low byte 0x00 reserved configuration byte 0 0x01 ... 0x07 reserved reserved table 15-6. configuration byte 0 bit bit name description default value 7:3 ? reserved 1 (unprogrammed) 2 ckout system clock output 1 (unprogrammed) 1 wdton watchdog timer always on 1 (unprogrammed) 0 rstdisbl external reset disable 1 (unprogrammed)
110 8127c?avr?10/09 attiny4/5/9/10 configuration bits are not affected by a chip er ase but they can be cleared using the configura- tion section erase command (see ?erasing the configuration section? on page 113 ). note that configuration bits are locked if non-volatile lock bit 1 (nvlb1) is programmed. 15.3.3.1 latching of configuration bits all configuration bits are latched either when the device is reset or when the device exits the external programming mode. changes to configurat ion bit values have no effect until the device leaves the external programming mode. 15.3.4 signature section the signature section is a dedicated memory area used for storing miscellaneous device infor- mation, such as the device signature. most of this memory section is reserved for internal use, as shown in table 15-7 . attiny4/5/9/10 have a three-byte signature code, which can be us ed to identify the device. the three bytes reside in the signature section, as shown in table 15-7 . the signature data for attiny4/5/9/10 is given in table 15-8 . 15.3.5 calibration section attiny4/5/9/10 have one calibration byte. the ca libration byte contains the calibration data for the internal oscillator and resides in the calibration section, as shown in table 15-9 . during reset, the calibration byte is auto matically written into the osccal register to ensure correct fre- quency of the calibrat ed internal oscillator. table 15-7. signature bytes signature word address signature word data high byte low byte 0x00 device id 1 manufacturer id 0x01 reserved for internal use device id 2 0x02 ... 0x0f reserved for internal use reserved for internal use table 15-8. signature codes part signature bytes manufacturer id device id 1 device id 2 attiny4 0x1e 0x8f 0x0a attiny5 0x1e 0x8f 0x09 attiny9 0x1e 0x90 0x08 ATTINY10 0x1e 0x90 0x03 table 15-9. calibration byte calibration word address calibration word data high byte low byte 0x00 reserved internal oscillator calibration value 0x01 ... 0x07 reserved reserved
111 8127c?avr?10/09 attiny4/5/9/10 15.3.5.1 latching of calibration value to ensure correct frequency of th e calibrated internal oscillator the calibrati on value is automati- cally written into the oscca l register during reset. 15.4 accessing the nvm nvm lock bits, and all flash memory sections are mapped to the data space as shown in figure 5-1 on page 15 . the nvm can be accessed for read and programming via the locations mapped in the data space. the nvm controller recognises a set of commands that can be used to instruct the controller what type of programming task to perform on the nvm. commands to the nvm controller are issued via the nvm command register. see ?nvmcmd - non-volatile memory command reg- ister? on page 115 . after the selected command has been loaded, the operation is started by writing data to the nvm locations mapped to the data space. when the nvm controller is busy performing an oper ation it will signal this via the nvm busy flag in the nvm control and status register. see ?nvmcsr - non-volatile memory control and status register? on page 115 . the nvm command register is bl ocked for write access as long as the busy flag is active. this is to ensure that the current command is fully executed before a new command can start. programming any part of th e nvm will automatically inhibi t the following operations: ? all programming to any other part of the nvm ? all reading from any nvm location attiny4/5/9/10 support only external programming. internal programming operations to nvm have been disabled, which means any internal a ttempt to write or erase nvm locations will fail. 15.4.1 addressing the flash the data space uses byte access ing but since the flash secti ons are accessed as words and organized in pages, the byte-address of the data space must be converted to the word-address of the flash section. this is illustrated in figure 15-1 . also, see table 15-3 on page 109 . the most significant bits of the data space address select the nvm lock bits or the flash sec- tion mapped to the data memory. the word addr ess within a page (waddr) is held by bits [waddrmsb:1], and the page address (paddr) by bits [paddrmsb:waddrmsb+1]. together, paddr and waddr form the absolute address of a word in the flash section. the least significant bit of the flash section addre ss is used to select the low or high byte of the word.
112 8127c?avr?10/09 attiny4/5/9/10 figure 15-1. addressing the flash memory 15.4.2 reading the flash the flash can be read from the data memory mapped locations one byte at a time. for read operations, the least significant bit (bit 0) is us ed to select the low or high byte in the word address. if this bit is zero, the low byte is re ad, and if it is one, the high byte is read. 15.4.3 programming the flash the flash can be written word-by-word. before wr iting a flash word, the flash target location must be erased. writing to an un-erased flash word will corrupt its content. the flash is word-accessed for writing, and the data space uses byte-addressing to access flash that has been mapped to data memory. it is t herefore important to write the word in the correct order to the flash, namely low bytes before high bytes. first, the low byte is written to the temporary buffer. then, writing the high byte latches both the high byte and the low byte into the flash word buffer, starting the write operation to flash. the flash erase operations can only pe rformed for the entire flash sections. the flash programming sequence is as follows: 1. perform a flash section erase or perform a chip erase 2. write the flash section word by word 15.4.3.1 chip erase the chip erase command will erase the entire code section of the flash memory and the nvm lock bits. for security reasons, the nvm lock bits are not reset before the code section has been completely erased. configuration, signatu re and calibration sections are not changed. 00 pag e 01 02 sectionend word 00 01 pageend flash section flash pag e 0/1 waddr paddr 1 waddrmsb paddrmsb 16 address pointer ... ... ... ... ... ... page address within a flash section word address within a flash pag e low/high byte select waddrmsb+1
113 8127c?avr?10/09 attiny4/5/9/10 before starting the chip erase, the nvmcmd register must be loaded with the chip_erase command. to start the erase operat ion a dummy byte must be writ ten into the high byte of a word location that resides insi de the flash code section. the nv mbsy bit remains set until eras- ing has been completed. while the flash is being erased neither flash buffer loading nor flash reading can be performed. the chip erase can be carried out as follows: 1. write the chip_erase command to the nvmcmd register 2. start the erase operation by writing a dummy byte to the high byte of any word location inside the code section 3. wait until the nvmbsy bit has been cleared 15.4.3.2 erasing the code section the algorithm for erasing all pages of the flash code section is as follows: 1. write the section_erase command to the nvmcmd register 2. start the erase operation by writing a dummy byte to the high byte of any word location inside the code section 3. wait until the nvmbsy bit has been cleared 15.4.3.3 writing a code word the algorithm for writing a word to the code section is as follows: 1. write the word_write command to the nvmcmd register 2. write the low byte of the data into the low byte of a word location 3. write the high byte of the data into the hi gh byte of the same word location. this will start the flash write operation 4. wait until the nvmbsy bit has been cleared 15.4.3.4 erasing the configuration section the algorithm for erasing the configuration section is as follows: 1. write the section_erase command to the nvmcmd register 2. start the erase operation by writing a dummy byte to the high byte of any word location inside the configuration section 3. wait until the nvmbsy bit has been cleared 15.4.3.5 writing a configuration word the algorithm for writing a conf iguration word is as follows. 1. write the word_write command to the nvmcmd register 2. write the low byte of the data to the low byte of a configuration word location 3. write the high byte of the data to the high byte of the same configuration word location. this will start the flash write operation. 4. wait until the nvmbsy bit has been cleared
114 8127c?avr?10/09 attiny4/5/9/10 15.4.4 reading nvm lock bits the non-volatile memory lock byte can be read from the mapped location in data memory. 15.4.5 writing nvm lock bits the algorithm for writing the lock bits is as follows. 1. write the word_write command to the nvmcmd register. 2. write the lock bits value to th e non-volatile memory lock by te location. this is the low byte of the non-volatile memory lock word. 3. start the nvm lock bit write operation by wr iting a dummy byte to the high byte of the nvm lock word location. 4. wait until the nvmbsy bit has been cleared. 15.5 self programming the attiny4/5/9/10 don't s upport internal programming. 15.6 external programming the method for programming the non-volatile me mories by means of an external programmer is referred to as external programming. external programming can be done both in-system or in mass production. the non-volatile memories can be externally programmed via the tiny programming interface (tpi). for details on the tpi, see ?programming interface? on page 96 . using the tpi, the exter- nal programmer can access the nvm control and status registers mapped to i/o space and the nvm memory mapped to data memory space. 15.6.1 entering external programming mode the tpi must be enabled before external programming mode can be entered. the following pro- cedure describes, how to enter the external programming mode after the tpi has been enabled: 1. make a request for enabling nvm programming by sending the nvm memory access key with the skey instruction. 2. poll the status of the nvmen bit in tpisr until it has been set. refer to the tiny programming interface description on page 96 for more detailed information of enabling the tpi and programming the nvm. 15.6.2 exiting extern al programming mode clear the nvm enable bit to disable nvm programming, then release the reset pin. see nvmen bit in ?tpisr ? tiny programming interface status register? on page 106 .
115 8127c?avr?10/09 attiny4/5/9/10 15.7 register description 15.7.1 nvmcsr - non-volatile memo ry control and status register ? bit 7 - nvmbsy: non-volatile memory busy this bit indicates the nvm memory (flash me mory and lock bits) is busy, being programmed. this bit is set when a program operation is star ted, and it remains set until the operation has been completed. ? bit 6:0 - res: reserved bits these bits are reserved and will always be read as zero. 15.7.2 nvmcmd - non-volatile memory command register ? bit 7:6 - res: reserved bits these bits are reserved and will always read zero. ? bit 5:0 - nvmcmd[5:0]: non-volatile memory command these bits define the programming commands for the flash, as shown in table 15-10 . bit 7 6543210 0x32 nvmbsy ? ? ? ? ? ? ?nvmcsr read/writer/wrrrrrrr initial value0 0000000 bit 76543210 0x33 ? ? nvmcmd[5:0] nvmcmd read/write r r r/w r/w r/w r/w r/w r/w initial value00000000 table 15-10. nvm programming commands operation type nvmcmd mnemonic description binary hex general 0b000000 0x00 no_operation no operation 0b010000 0x10 chip_erase chip erase section 0b010100 0x14 section_erase section erase word 0b011101 0x1d word_write word write
116 8127c?avr?10/09 attiny4/5/9/10 16. electrical characteristics 16.1 absolute maximum ratings* 16.2 dc characteristics operating temperature.................................. -55 c to +125 c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of th is specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ..................................... -65c to +150c voltage on any pin except reset with respect to ground ................................-0.5v to v cc +0.5v voltage on reset with respect to ground......-0.5v to +13.0v maximum operating voltage ............................................ 6.0v dc current per i/o pin ............................................... 40.0 ma dc current v cc and gnd pins................................ 200.0 ma table 16-1. dc characteristics. t a = -40 c to +85 c symbol parameter condition min. typ. max. units v il input low voltage v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v -0.5 0.2v cc 0.3v cc v v ih input high-voltage except reset pin v cc = 1.8v - 2.4v v cc = 2.4v - 5.5v 0.7v cc (1) 0.6v cc (1) v cc +0.5 (2) v input high-voltage reset pin v cc = 1.8v to 5.5v 0.9v cc (1) v cc +0.5 (2) v v ol output low voltage (3) except reset pin (5) i ol = 10 ma, v cc = 5v i ol = 5 ma, v cc = 3v 0.6 0.5 v v oh output high-voltage (4) except reset pin (5) i oh = -10 ma, v cc = 5v i oh = -5 ma, v cc = 3v 4.3 2.5 v i lil input leakage current i/o pin vcc = 5.5 v, pin low (absolute value) <0.05 1 a i lih input leakage current i/o pin vcc = 5.5 v, pin high (absolute value) <0.05 1 a r rst reset pull-up resistor vcc = 5.5 v, input low 30 60 k r pu i/o pin pull-up resistor vcc = 5.5 v, input low 20 50 k i aclk analog comparator input leakage current v cc = 5v v in = v cc /2 -50 50 na
117 8127c?avr?10/09 attiny4/5/9/10 notes: 1. ?min? means the lowest value wher e the pin is guaranteed to be read as high. 2. ?max? means the highest value where the pin is guaranteed to be read as low. 3. although each i/o port can sink more than the test conditions (10 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient ), the sum of all i ol (for all ports) should not exceed 60 ma. if i ol exceeds the test conditions, v ol may exceed the related specification. pi ns are not guaranteed to sink current greater than the list ed test condition. 4. although each i/o port can source more than the test conditions (10 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient ), the sum of all i oh (for all ports) should not exceed 60 ma. if i oh exceeds the test condition, v oh may exceed the related specification. pins are not guaranteed to source current gr eater than the listed test condition. 5. the reset pin must tolerate high voltages when entering and op erating in programming modes and, as a consequence, has a weak drive strength as compared to regular i/o pins. see figure 17-25 on page 135 , and figure 17-26 on page 135 . 6. values are with external clo ck using methods described in ?minimizing power consumption? on page 24 . power reduction is enabled (prr = 0xff) and there is no i/o drive. 7. bod disabled. 16.3 speed grades the maximum operating frequency of the device depends on v cc . as shown in figure 16-1 , the relationship between maximum frequency vs. v cc is linear between 1.8v < v cc < 4.5v. figure 16-1. maximum frequency vs. v cc i cc power supply current (6) active 1mhz, v cc = 2v 0.2 0.5 ma active 4mhz, v cc = 3v 0.8 1.5 ma active 8mhz, v cc = 5v 2.7 5 ma idle 1mhz, v cc = 2v 0.02 0.2 ma idle 4mhz, v cc = 3v 0.13 0.5 ma idle 8mhz, v cc = 5v 0.6 1.5 ma power-down mode (7) wdt enabled, v cc = 3v 4.5 10 a wdt disabled, v cc = 3v 0.15 2 a table 16-1. dc characteristics. t a = -40 c to +85 c (continued) symbol parameter condition min. typ. max. units 4 mhz 8 mhz 1.8v 5.5v 4.5v 2.7v 12 mhz
118 8127c?avr?10/09 attiny4/5/9/10 16.4 clock characteristics 16.4.1 accuracy of calibra ted internal oscillator it is possible to manua lly calibrate the internal oscillator to be more accu rate than def ault factory calibration. note that the osc illator frequency depend s on temperat ure and voltage. voltage and temperature characteristics can be found in figure 17-39 on page 142 and figure 17-40 on page 142 . notes: 1. accuracy of oscillator frequency at calibrat ion point (fixed temperature and fixed voltage). 16.4.2 external clock drive figure 16-2. external clock drive waveform table 16-2. calibration accuracy of internal rc oscillator calibration method target frequency v cc temperature accuracy at given voltage & temperature (1) factory calibration 8.0 mhz 3v 25 c10% user calibration fixed frequency within: 7.3 ? 8.1 mhz fixed voltage within: 1.8v ? 5.5v fixed temp. within: -40 c ? 85 c 1% v il1 v ih1 table 16-3. external clock driv e characteristics symbol parameter v cc = 1.8 - 5.5v v cc = 2.7 - 5.5v v cc = 4.5 - 5.5v units min. max. min. max. min. max. 1/t clcl clock frequency 0 4 0 8 0 12 mhz t clcl clock period 250 125 83 ns t chcx high time 100 50 33 ns t clcx low time 100 50 33 ns t clch rise time 2.0 1 0.6 s t chcl fall time 2.0 1 0.6 s t clcl change in period fr om one clock cycle to the next 2 2 2 %
119 8127c?avr?10/09 attiny4/5/9/10 16.5 system and reset characteristics note: 1. values are guidelines, only 16.5.1 power-on reset note: 1. values are guidelines, only 2. threshold where device is released from reset when voltage is rising 3. the power-on reset will not work unless the supply voltage has been below v pot (falling) 16.5.2 v cc level monitor note: 1. typical values at room temperature table 16-4. reset, vlm, and internal voltage characteristics symbol parameter condition min (1) typ (1) max (1) units v rst reset pin threshold voltage 0.2 v cc 0.9v cc v t rst minimum pulse width on reset pin v cc = 1.8v v cc = 3v v cc = 5v 2000 700 400 ns t tout time-out after reset 64 128 ms v hyst vlm hysteresis 50 mv table 16-5. characteristics of enhanced power-on reset. t a = -40 - 85 c symbol parameter min (1) typ (1) max (1) units v por release threshold of power-on reset (2) 1.1 1.4 1.6 v v poa activation threshold of power-on reset (3) 0.6 1.3 1.6 v sr on power-on slope rate 0.01 v/ms table 16-6. voltage level monitor thresholds parameter min typ (1) max units trigger level vlm1l 1.1 1.4 1.6 v trigger level vlm1h 1.4 1.6 1.8 trigger level vlm2 2.0 2.5 2.7 trigger level vlm3 3.2 3.7 4.5 settling time vmlm2,vlm3 (vlm1h,vlm1l) 5 (50) s
120 8127c?avr?10/09 attiny4/5/9/10 16.6 analog comparat or characteristics note: all parameters are based on simulation results. none are tested in production 16.7 adc characteristics (attiny5/10, only) table 16-7. analog comparator characteristics, t a = -40 c - 85 c symbol parameter condition min typ max units v aio input offset voltage v cc = 5v, v in = v cc / 2 < 10 40 mv i lac input leakage current v cc = 5v, v in = v cc / 2 -50 50 na t apd analog propagation delay (from saturation to slight overdrive) v cc = 2.7v 750 ns v cc = 4.0v 500 analog propagation delay (large step change) v cc = 2.7v 100 v cc = 4.0v 75 t dpd digital propagation delay v cc = 1.8v - 5.5 1 2 clk table 16-8. adc characteristics. t = -40 c ? 85 c. v cc = 2.5v ? 5.5v symbol parameter condition min typ max units resolution 8bits absolute accuracy (including inl, dnl, and quantization, gain and offset errors) v ref = v cc = 4v, adc clock = 200 khz 1.0 lsb v ref = v cc = 4v, adc clock = 200 khz noise reduction mode 1.0 lsb integral non-linearity (inl) (accuracy after offset and gain calibration) v ref = v cc = 4v, adc clock = 200 khz 1.0 lsb differential non-linearity (dnl) v ref = v cc = 4v, adc clock = 200 khz 0.5 lsb gain error v ref = v cc = 4v, adc clock = 200 khz 1.0 lsb offset error v ref = v cc = 4v, adc clock = 200 khz 1.0 lsb conversion time free running conversion 65 260 s clock frequency 50 200 khz v in input voltage gnd v ref v input bandwidth 7.7 khz r ain analog input resistance 100 m adc conversion output 0 255 lsb
121 8127c?avr?10/09 attiny4/5/9/10 16.8 serial programming characteristics figure 16-3. serial programming timing table 16-9. serial programming characteristics, t a = -40 c to 85 c, v cc = 5v (unless other- wise noted) symbol parameter min typ max units 1/t clcl clock frequency 2 mhz t clcl clock period 500 ns t clch clock low pulse width 200 ns t chch clock high pulse width 200 ns t ivch data input to clock high setup time 50 ns t chix data input hold time after clock high 100 ns t clov data output valid after clock low time 200 ns t chix tpidata t ivch t chcl t clch t clcl tpiclk t clov transmit mode receive mode
122 8127c?avr?10/09 attiny4/5/9/10 17. typical characteristics the data contained in this section is largely ba sed on simulations and characterization of similar devices in the same process and design methods. thus, the data should be treated as indica- tions of how the part will behave. the following charts show typical behavior. t hese figures are not tested during manufacturing. during characterisation devices are operated at fr equencies higher than test limits but they are not guaranteed to function properly at frequenc ies higher than the ordering code indicates. all current consumption measurements are perfor med with all i/o pins configured as inputs and with internal pull-ups enabled. current consumption is a function of several factors such as oper- ating voltage, operating frequency, loading of i/o pins, switching rate of i/o pins, code executed and ambient temperature. the dominating factors are operating voltage and frequency. a sine wave generator with rail-to-rail output is used as clock source but current consumption in power-down mode is independent of clock select ion. the difference between current consump- tion in power-down mode with watchdog timer enabled and power-down mode with watchdog timer disabled represents the differenti al current drawn by the watchdog timer. the current drawn from pins with a capacitive lo ad may be estimated (for one pin) as follows: where v cc = operating voltage, c l = load capacitance and f sw = average switching frequency of i/o pin. 17.1 supply current of i/o modules tables and formulas below can be used to calcul ate additional current consumption for the dif- ferent i/o modules in active and idle mode. en abling and disabling of i/o modules is controlled by the power reduction register. see ?power reduction register? on page 24 for details. note: 1. the adc is available in attiny5/10, only table 17-2 below can be used for calculating typical current consumption for other supply volt- ages and frequencies than those mentioned in the table 17-1 above. note: 1. the adc is available in attiny5/10, only i cp v cc c l f sw table 17-1. additional current consumption for the different i/o modules (absolute values) prr bit typical numbers v cc = 2v, f = 1mhz v cc = 3v, f = 4mhz v cc = 5v, f = 8mhz prtim0 6.6 ua 40.0 ua 153.0 ua pradc (1) 29.6 ua 88.3 ua 333.3 ua table 17-2. additional current consumption (percentage) in active and idle mode prr bit current consumption additional to active mode with external clock (see figure 17-1 and figure 17-2 ) current consumption additional to idle mode with external clock (see figure 17-7 and figure 17-8 ) prtim0 2.3 % 10.4 % pradc (1) 6.7 % 28.8 %
123 8127c?avr?10/09 attiny4/5/9/10 17.2 attiny4/5/9/10 17.2.1 active supply current figure 17-1. active supply current vs. low frequency (0.1 - 1.0 mhz) figure 17-2. active supply current vs. frequency (1 - 12 mhz) active supply current v s . low frequency (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 fre qu ency (mhz) i cc (ma) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v active supply current v s . frequency (prr=0xff) 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 024681012 fre qu ency (mhz) i cc (ma)
124 8127c?avr?10/09 attiny4/5/9/10 figure 17-3. active supply current vs. v cc (internal oscillator, 8 mhz) figure 17-4. active supply current vs. v cc (internal oscillator, 1 mhz) active supply current v s . v cc internal oscillator, 8 mhz 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 3.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) active supply current v s . v cc internal oscillator, 1 mhz 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
125 8127c?avr?10/09 attiny4/5/9/10 figure 17-5. active supply current vs. v cc (internal oscillator, 128 khz) figure 17-6. active supply current vs. v cc (external clock, 32 khz) active supply current v s . v cc internal oscillator, 128 khz 85 c 25 c -40 c 0 0.02 0.04 0.06 0.08 0.1 0.12 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) active supply current v s . v cc internal oscillator, 32 khz 85 c 25 c -40 c 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
126 8127c?avr?10/09 attiny4/5/9/10 17.2.2 idle supply current figure 17-7. idle supply current vs. low frequency (0.1 - 1.0 mhz) figure 17-8. idle supply current vs. frequency (1 - 12 mhz) idle supply current vs. low frequency (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0,01 0,02 0,03 0,04 0,05 0,06 0,07 0,08 0,09 0,1 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 frequency (mhz) i cc (ma) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v idle supply current vs. frequency (prr=0xff) 0 0,2 0,4 0,6 0,8 1 2 1 0 1 8 6 4 2 0 frequency (mhz) i cc (ma)
127 8127c?avr?10/09 attiny4/5/9/10 figure 17-9. idle supply current vs. v cc (internal osc illator, 8 mhz) figure 17-10. idle supply current vs. v cc (internal osc illator, 1 mhz) idle supply current vs. v cc internal rc oscillator, 8 mhz 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma) idle supply current vs. v cc internal rc oscillator, 1 mhz 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ma)
128 8127c?avr?10/09 attiny4/5/9/10 17.2.3 power-down supply current figure 17-11. power-down supply current vs. v cc (watchdog timer disabled) figure 17-12. power-down supply current vs. v cc (watchdog timer enabled) power-down supply current v s . v cc watchdog timer disabled 85 c 25 c -40 c 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc ( u a) power-down supply current vs. v cc watchdog timer enabled 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 9 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua)
129 8127c?avr?10/09 attiny4/5/9/10 17.2.4 pin pull-up figure 17-13. i/o pin pull-up resistor cu rrent vs. input voltage (v cc = 1.8v) figure 17-14. i/o pin pull-up resistor current vs. input voltage (v cc = 2.7v) i/o pin pull-up resistor current vs. input voltage 85 c 25 c -40 c 0 10 20 30 40 50 60 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 v op (v) i op (ua) i/o pin pull-up resistor current vs. input voltage 85 c 25 c -40 c 0 10 20 30 40 50 60 70 80 3 5 , 2 2 5 , 1 1 5 , 0 0 v op (v) i op (ua)
130 8127c?avr?10/09 attiny4/5/9/10 figure 17-15. i/o pin pull-up resistor cu rrent vs. input voltage (v cc = 5v) figure 17-16. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 1.8v) i/o pin pull-up resistor current vs. input voltage 85 c 25 c -40 c 0 20 40 60 80 100 120 140 160 6 5 4 3 2 1 0 v op (v) i op (ua) reset pull-up resistor current vs. reset pin voltage 85 c 25 c -40 c 0 5 10 15 20 25 30 35 40 0 0,2 0,4 0,6 0,8 1 1,2 1,4 1,6 1,8 2 v reset (v) i reset (ua)
131 8127c?avr?10/09 attiny4/5/9/10 figure 17-17. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 2.7v) figure 17-18. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 5v) reset pull-up resistor current vs. reset pin voltage 85 c 25 c -40 c 0 10 20 30 40 50 60 3 5 , 2 2 5 , 1 1 5 , 0 0 v reset (v) i reset (ua) reset pull-up resistor current vs. reset pin voltage 85 c 25 c -40 c 0 20 40 60 80 100 120 00,511,522,533,544,55 v reset (v) i reset (ua)
132 8127c?avr?10/09 attiny4/5/9/10 17.2.5 pin driver strength figure 17-19. i/o pin output voltage vs. sink current (v cc = 1.8v) figure 17-20. i/o pin output voltage vs. sink current (v cc = 3v) i/o pin output voltage v s . sink current v cc = 1.8v 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i ol (ma) v ol (v) i/o pin output voltage v s . sink current v cc = 3v 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 012345678910 i ol (ma) v ol (v)
133 8127c?avr?10/09 attiny4/5/9/10 figure 17-21. i/o pin output voltage vs. sink current (v cc = 5v) figure 17-22. i/o pin output voltage vs. source current (v cc = 1.8v) i/o pin output voltage v s . sink current v cc = 5v 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 0 2 4 6 8 10 12 14 16 18 20 i ol (ma) v ol (v) i/o pin output voltage v s . source current v cc = 1.8v 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i oh (ma) v oh (v)
134 8127c?avr?10/09 attiny4/5/9/10 figure 17-23. i/o pin output voltage vs. source current (v cc = 3v) figure 17-24. i/o pin output voltage vs. source current (v cc = 5v) i/o pin output voltage v s . source current v cc = 3v 85 c 25 c -40 c 1.5 1.7 1.9 2.1 2.3 2.5 2.7 2.9 3.1 012345678910 i oh (ma) v oh (v) i/o pin output voltage v s . source current v cc = 5v 85 c 25 c -40 c 4 4.2 4.4 4.6 4.8 5 5.2 0 2 4 6 8 10 12 14 16 18 20 i oh (ma) v oh (v)
135 8127c?avr?10/09 attiny4/5/9/10 figure 17-25. reset pin as i/o, output voltage vs. sink current figure 17-26. reset pin as i/o, output vo ltage vs. source current output voltage v s . sink current reset pin as i/o 5.0 v 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 4 3 2 1 0 i ol (ma) v ol (v) 3.0 v 1.8 v output voltage v s . source current reset pin as i/o 5.0 v 3.0 v 0 1 2 3 4 5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 i oh (ma) v oh (v) 1.8 v
136 8127c?avr?10/09 attiny4/5/9/10 17.2.6 pin threshold and hysteresis figure 17-27. i/o pin input threshold voltage vs. v cc (v ih , io pin read as ?1?) figure 17-28. i/o pin input threshold voltage vs. v cc (v il , io pin read as ?0?) i/o pin input threshold voltage vs. v cc vih, io pin read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 3 3,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) i/o pin input threshold voltage vs. v cc vil, io pin read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 3 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
137 8127c?avr?10/09 attiny4/5/9/10 figure 17-29. i/o pin input hysteresis vs. v cc figure 17-30. reset pin as i/o, input threshold voltage vs. v cc (v ih , i/o pin read as ?1?) i/o pin input hysteresis vs. v cc 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (v) reset pin as i/o threshold voltage vs. v cc vih, reset read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 3 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v)
138 8127c?avr?10/09 attiny4/5/9/10 figure 17-31. reset pin as i/o, input threshold voltage vs. v cc (v il , i/o pin read as ?0?) figure 17-32. reset input hysteresis vs. v cc (reset pin used as i/o) reset pin as i/o threshold voltage vs. v cc vil, reset read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) threshold (v) reset pin as i/o, input hysteresis vs. vcc v il , pin read as "0" 85 c 25 c -40 c 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) input hysteresis (mv)
139 8127c?avr?10/09 attiny4/5/9/10 figure 17-33. reset input threshold voltage vs. v cc (v ih , i/o pin read as ?1?) figure 17-34. reset input threshold voltage vs. v cc (v il , i/o pin read as ?0?) reset input threshold voltage vs. v cc vih, io pin read as '1' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,522,533,544,555,5 v cc (v) threshold (v) reset input threshold voltage vs. v cc vil, io pin read as '0' 85 c 25 c -40 c 0 0,5 1 1,5 2 2,5 1,522,533,544,555,5 v cc (v) threshold (v)
140 8127c?avr?10/09 attiny4/5/9/10 figure 17-35. reset pin, input hysteresis vs. v cc 17.2.7 analog comparator offset figure 17-36. analog comparator offset reset pin input hysteresis vs. v cc 85 c 25 c -40 c 0 0,2 0,4 0,6 0,8 1 1,522,533,544,555,5 v cc (v) input hysteresis (mv) analog comparator offset vcc = 5v -0,002 0 0,002 0,004 0,006 0 0,5 1 1,5 2 2,5 3 3,5 4 4,5 5 vin offset 85 c 25 c -40 c
141 8127c?avr?10/09 attiny4/5/9/10 17.2.8 internal oscillator speed figure 17-37. watchdog oscillato r frequency vs. v cc figure 17-38. watchdog oscillator freq uency vs. temperature watchdog oscillator frequency v s . operating voltage 85 c 25 c -40 c 99 100 101 102 103 104 105 106 107 108 109 110 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) fre qu ency (khz) watchdog oscillator frequency v s . temperature 5.5 v 4.0 v 3.3 v 2.7 v 1.8 v 100 101 102 103 104 105 106 107 108 109 110 -60 -40 -20 0 20 40 60 80 100 temper a t u re fre qu ency (khz)
142 8127c?avr?10/09 attiny4/5/9/10 figure 17-39. calibrated oscillator frequency vs. v cc figure 17-40. calibrated oscillator freq uency vs. temperature calibrated 8.0mhz oscillator frequency v s . operating voltage 85 c 25 c -40 c 7.4 7.6 7.8 8 8.2 8.4 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) fre qu ency (mhz) calibrated 8.0mhz oscillator frequency v s . temperature 5.0 v 3.0 v 1.8 v 7.6 7.7 7.8 7.9 8 8.1 8.2 8.3 -40 -20 0 20 40 60 80 100 temper a t u re fre qu ency (mhz)
143 8127c?avr?10/09 attiny4/5/9/10 figure 17-41. calibrated oscillator freq uency vs, osccal value 17.2.9 vlm thresholds figure 17-42. vlm1l threshold of v cc level monitor calibrated 8.0mhz rc oscillator frequency v s . osccal value v cc = 3v 85 c 25 c -40 c 0 2 4 6 8 10 12 14 16 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 osccal (x1) fre qu ency (mhz) vlm threshold v s . temperature vlm2:0 = 001 1.34 1.35 1.36 1.37 1.38 1.39 1.4 1.41 1.42 -40 -20 0 20 40 60 80 100 temper a t u re (c) thre s hold (v)
144 8127c?avr?10/09 attiny4/5/9/10 figure 17-43. vlm1h threshold of v cc level monitor figure 17-44. vlm2 threshold of v cc level monitor vlm threshold v s . temperature vlm2:0 = 010 1.4 1.45 1.5 1.55 1.6 1.65 1.7 -40 -20 0 20 40 60 80 100 temper a t u re (c) thre s hold (v) vlm threshold v s . temperature vlm2:0 = 011 2.43 2.44 2.45 2.46 2.47 2.48 -40 -20 0 20 40 60 80 100 temper a t u re (c) thre s hold (v)
145 8127c?avr?10/09 attiny4/5/9/10 figure 17-45. vlm3 threshold of v cc level monitorr2 17.2.10 current consumption of peripheral units figure 17-46. adc current vs. v cc (attiny5/10, only) vlm threshold v s . temperature vlm2:0 = 100 3.4 3.5 3.6 3.7 3.8 3.9 -40 -20 0 20 40 60 80 100 temper a t u re (c) thre s hold (v) adc current v s . v cc 4.0 mhz frequency 0 100 200 300 400 500 600 700 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc ( u a)
146 8127c?avr?10/09 attiny4/5/9/10 figure 17-47. analog comparator current vs. v cc figure 17-48. v cc level monitor current vs. v cc analog comparator current vs. v cc 0 20 40 60 80 100 120 140 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua) 25 ?c vlm supply current v s . v cc vlm2:0 = 100 vlm2:0 = 011 vlm2:0 = 010 vlm2:0 = 001 vlm2:0 = 000 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
147 8127c?avr?10/09 attiny4/5/9/10 figure 17-49. temperature dependence of vlm current vs. v cc figure 17-50. watchdog timer current vs. v cc vlm supply current v s . v cc vlm2:0 = 001 85 c 25 c -40 c 0 50 100 150 200 250 300 350 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc ( u a) 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 9 1,5 2 2,5 3 3,5 4 4,5 5 5,5 v cc (v) i cc (ua) watchdog timer current vs. v cc
148 8127c?avr?10/09 attiny4/5/9/10 17.2.11 current consumption in reset and reset pulsewidth figure 17-51. reset supply current vs. v cc (0.1 - 1.0 mhz, excluding current through the reset pull-up) figure 17-52. minimum reset pulse width vs. v cc tbd minimum reset pulse width vs. v cc 85 c 25 c -40 c 0 500 1000 1500 2000 2500 1,522,533,544,555,5 v cc (v) pulsewidth (ns)
149 8127c?avr?10/09 attiny4/5/9/10 18. register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page 0x3f sreg i t h s v n z c page 12 0x3e sph stack pointer high byte page 12 0x3d spl stack pointer low byte page 12 0x3c ccp cpu change protection byte page 12 0x3b rstflr ? ? ? ?wdrf ? extrf porf page 34 0x3a smcr ? ? ? ? sm2 sm1 sm0 se page 25 0x39 osccal oscillator calibration byte page 21 0x38 reserved ? ? ? ? ? ? ? ? 0x37 clkmsr ? ? ? ? ? ? clkms1 clkms0 page 21 0x36 clkpsr ? ? ? ? clkps3 clkps2 clkps1 clkps0 page 22 0x35 prr ? ? ? ? ? ? pradc prtim0 page 26 0x34 vlmcsr vlmf vlmie ? ? ? vlm2 vlm1 vlm0 page 33 0x33 nvmcmd ? ? nvm comman page 115 0x32 nvmcsr nvmbsy ? ? ? ? ? ? ? page 115 0x31 wdtcsr wdif wdie wdp3 ? wde wdp2 wdp1 wdp0 page 32 0x30 reserved ? ? ? ? ? ? ? ? 0x2f gtccr tsm ? ? ? ? ? ?psr page 79 0x2e tccr0a com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 page 73 0x2d tccr0b icnc0 ices0 ? wgm03 wgm02 cs02 cs01 cs00 page 75 0x2c tccr0c foc0a foc0b ? ? ? ? ? ? page 76 0x2b timsk0 ? ?icie0 ? ? ocie0b ocie0a toie0 page 78 0x2a tifr0 ? ?icf0 ? ? ocf0b ocf0a tov0 page 79 0x29 tcnt0h timer/counter0 ? counter register high byte page 77 0x28 tcnt0l timer/counter0 ? counter register low byte page 77 0x27 ocr0ah timer/counter0 ? compare register a high byte page 77 0x26 ocr0al timer/counter0 ? compare register a low byte page 77 0x25 ocr0bh timer/counter0 ? compare register b high byte page 77 0x24 ocr0bl timer/counter0 ? compare register b low byte page 77 0x23 icr0h timer/counter0 - input capture register high byte page 78 0x22 icr0l timer/counter0 - input capture register low byte page 78 0x21 reserved ? ? ? ? ? ? ? ? 0x20 reserved ? ? ? ? ? ? ? ? 0x1f acsr acd ? aco aci acie acic acis1 acis0 page 81 0x1e reserved ? ? ? ? ? ? ? ? 0x1d adcsra aden adsc adate adif adie adps2 adps1 adps0 page 93 0x1c adcsrb ? ? ? ? ? adts2 adts1 adts0 page 94 0x1b admux ? ? ? ? ? ? mux1 mux0 page 93 0x1a reserved ? ? ? ? ? ? ? ? 0x19 adcl adc conversion result page 95 0x18 reserved ? ? ? ? ? ? ? ? 0x17 didr0 ? ? ? ? adc3d adc2d adc1d adc0d page 82, page 95 0x16 reserved ? ? ? ? ? ? ? ? 0x15 eicra ? ? ? ? ? ? isc01 isc00 page 37 0x14 eifr ? ? ? ? ? ? ? intf0 page 38 0x13 eimsk ? ? ? ? ? ? ? int0 page 38 0x12 pcicr ? ? ? ? ? ? ? pcie0 page 39 0x11 pcifr ? ? ? ? ? ? ?pcif0 page 39 0x10 pcmsk ? ? ? ? pcint3 pcint2 pcint1 pcint0 page 39 0x0f reserved ? ? ? ? ? ? ? ? 0x0e reserved ? ? ? ? ? ? ? ? 0x0d reserved ? ? ? ? ? ? ? ? 0x0c portcr ? ? ? ? ? ? bbmb ? page 50 0x0b reserved ? ? ? ? ? ? ? ? 0x0a reserved ? ? ? ? ? ? ? ? 0x09 reserved ? ? ? ? ? ? ? ? 0x08 reserved ? ? ? ? ? ? ? ? 0x07 reserved ? ? ? ? ? ? ? ? 0x06 reserved ? ? ? ? ? ? ? ? 0x05 reserved ? ? ? ? ? ? ? ? 0x04 reserved ? ? ? ? ? ? ? ? 0x03 pueb ? ? ? ? pueb3 pueb2 pueb1 pueb0 page 50 0x02 portb ? ? ? ? portb3 portb2 portb1 portb0 page 51 0x01 ddrb ? ? ? ? ddrb3 ddrb2 ddrb1 ddrb0 page 51 0x00 pinb ? ? ? ? pinb3 pinb2 pinb1 pinb0 page 51
150 8127c?avr?10/09 attiny4/5/9/10 note: 1. for compatibility with future devices, reserved bits shou ld be written to zero if accesse d. reserved i/o memory addresse s should never be written. 2. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be ch ecked by using the sbis and sbic instructions. 3. some of the status flags are cleared by writing a logical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operation the specif ied bit, and can therefore be used on r egisters containing such status flags. the cbi and sbi instructions work wit h registers 0x00 to 0x1f only. 4. the adc is available in attiny5/10, only.
151 8127c?avr?10/09 attiny4/5/9/10 19. instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add without carry rd rd + rr z,c,n,v,s,h 1 adc rd, rr add with carry rd rd + rr + c z,c,n,v,s,h 1 sub rd, rr subtract without carry rd rd - rr z,c,n,v,s,h 1 subi rd, k subtract immediate rd rd - k z,c,n,v,s,h 1 sbc rd, rr subtract with carry rd rd - rr - c z,c,n,v,s,h 1 sbci rd, k subtract immediate with carry rd rd - k - c z,c,n,v,s,h 1 and rd, rr logical and rd rd ? rr z,n,v,s 1 andi rd, k logical and with immediate rd rd ? k z,n,v,s 1 or rd, rr logical or rd rd v rr z,n,v,s 1 ori rd, k logical or with immediate rd rd v k z,n,v,s 1 eor rd, rr exclusive or rd rd rr z,n,v,s 1 com rd one?s complement rd $ff ? rd z,c,n,v,s 1 neg rd two?s complement rd $00 ? rd z,c,n,v,s,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v,s 1 cbr rd,k clear bit(s) in register rd rd ? ($ffh - k) z,n,v,s 1 inc rd increment rd rd + 1 z,n,v,s 1 dec rd decrement rd rd ? 1 z,n,v,s 1 tst rd test for zero or minus rd rd ? rd z,n,v,s 1 clr rd clear register rd rd rd z,n,v,s 1 ser rd set register rd $ff none 1 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc(15:0) z, pc(21:16) 0none2 rcall k relative subroutine call pc pc + k + 1 none 3/4 icall indirect call to (z) pc(15:0) z, pc(21:16) 0none3/4 ret subroutine return pc stack none 4/5 reti interrupt return pc stack i 4/5 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, c,n,v,s,h 1 cpc rd,rr compare with carry rd ? rr ? c z, c,n,v,s,h 1 cpi rd,k compare with immediate rd ? k z, c,n,v,s,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic a, b skip if bit in i/o register cleared if (i/o(a,b)=0) pc pc + 2 or 3 none 1/2/3 sbis a, b skip if bit in i/o register is set if (i/o(a,b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 bit and bit-test instructions lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v,h 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v,h 1 ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1
152 8127c?avr?10/09 attiny4/5/9/10 bclr s flag clear sreg(s) 0 sreg(s) 1 sbi a, b set bit in i/o register i/o(a, b) 1none1 cbi a, b clear bit in i/o register i/o(a, b) 0none1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 cls clear signed test flag s 0 s 1 sev set two?s complement overflow. v 1v1 clv clear two?s complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 clh clear half carry flag in sreg h 0 h 1 data transfer instructions mov rd, rr copy register rd rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 1/2 ld rd, x+ load indirect and post-increment rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-decrement x x - 1, rd (x) none 2/3 ld rd, y load indirect rd (y) none 1/2 ld rd, y+ load indirect and post-increment rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-decrement y y - 1, rd (y) none 2/3 ld rd, z load indirect rd (z) none 1/2 ld rd, z+ load indirect and post-increment rd (z), z z+1 none 2 ld rd, -z load indirect and pre-decrement z z - 1, rd (z) none 2/3 lds rd, k store direct from sram rd ( k) none 1 st x, rr store indirect (x) rr none 1 st x+, rr store indirect and post-increment (x) rr, x x + 1 none 1 st - x, rr store indirect and pre-decrement x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 1 st y+, rr store indirect and post-increment (y) rr, y y + 1 none 1 st - y, rr store indirect and pre-decrement y y - 1, (y) rr none 2 st z, rr store indirect (z) rr none 1 st z+, rr store indirect and post-increment. (z) rr, z z + 1 none 1 st -z, rr store indirect and pre-decrement z z - 1, (z) rr none 2 sts k, rr store direct to sram (k) rr none 1 in rd, a in from i/o location rd i/o (a) none 1 out a, rr out to i/o location i/o (a) rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 mcu control instructions break break (see specific descr. for break) none 1 nop no operation none 1 sleep sleep (see specific descr. for sleep) none 1 wdr watchdog reset (see specific descr. for wdr) none 1 mnemonics operands description operation flags #clocks
153 8127c?avr?10/09 attiny4/5/9/10 20. ordering information notes: 1. this device can also be supplied in wafer form. please c ontact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging, complies to the european directive for restriction of hazardous substances (rohs directive). also halide free and fully green. 3. topside marking for attiny4: t4 x ( x stands for ?die revision?). 4. bottomside marking for attiny4: z h zzz [ h stands for (-40 c to 85 c)]. 20.1 attiny4 speed (mhz) power supply ordering code (2) package (1) operational range 12 1.8 - 5.5v attiny4-tshr (3)(4) 6st1 industrial (-40 c to 85 c) (4) package type 6st1 6-lead, 2.90 x 1.60 mm plastic small outline package (sot23)
154 8127c?avr?10/09 attiny4/5/9/10 notes: 1. this device can also be supplied in wafer form. please c ontact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging, complies to the european directive for restriction of hazardous substances (rohs directive). also halide free and fully green. 3. topside marking for attiny5: t5 x ( x stands for ?die revision?). 4. bottomside marking for attiny5: z h zzz [ h stands for (-40 c to 85 c)]. 20.2 attiny5 speed (mhz) power supply ordering code (2) package (1) operational range 12 1.8 - 5.5v attiny5-tshr (3)(4) 6st1 industrial (-40 c to 85 c) (4) package type 6st1 6-lead, 2.90 x 1.60 mm plastic small outline package (sot23)
155 8127c?avr?10/09 attiny4/5/9/10 notes: 1. this device can also be supplied in wafer form. please c ontact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging, complies to the european directive for restriction of hazardous substances (rohs directive). also halide free and fully green. 3. topside marking for attiny9: t9 x ( x stands for ?die revision?). 4. bottomside marking for attiny9: z h zzz [ h stands for (-40 c to 85 c)]. 20.3 attiny9 speed (mhz) power supply ordering code (2) package (1) operational range 12 1.8 - 5.5v attiny9-tshr (3)(4) 6st1 industrial (-40 c to 85 c) (4) package type 6st1 6-lead, 2.90 x 1.60 mm plastic small outline package (sot23)
156 8127c?avr?10/09 attiny4/5/9/10 notes: 1. this device can also be supplied in wafer form. please c ontact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging, complies to the european directive for restriction of hazardous substances (rohs directive). also halide free and fully green. 3. topside marking for ATTINY10: t10 x ( x stands for ?die revision?). 4. bottomside marking for ATTINY10: z h zzz [ h stands for (-40 c to 85 c)]. 20.4 ATTINY10 speed (mhz) power supply ordering code (2) package (1) operational range 12 1.8 - 5.5v ATTINY10-tshr (3)(4) 6st1 industrial (-40 c to 85 c) (4) package type 6st1 6-lead, 2.90 x 1.60 mm plastic small outline package (sot23)
157 8127c?avr?10/09 attiny4/5/9/10 21. packaging information 21.1 6st1 title drawing no. gpc rev. package drawing contact: p a ck a gedr a wing s @ a tmel.com 6st1 taq a 6st1, 6-lead, 2.90 x 1.60 mm plastic small outline package (sot23) max note symbol min nom common dimensions (unit of me asu re = mm) a ? ? 1.45 a1 0 ? 0.15 a2 0.90 ? 1.30 d 2.80 2.90 3.00 2 e 2.60 2.80 3.00 e1 1.50 1.60 1.75 l 0.30 0.45 0.55 e 0.95 bsc b 0.30 ? 0.50 3 c 0.09 ? 0.20 0 ? 8 notes: 1. this package is compliant with jedec specication mo-178 variation ab 2. dimension d does not include mold flash, protrusions or gate burrs. mold flash, protrustion or gate burrs shall not exceed 0.25 mm per end. 3. dimension b does not include dambar protrusion. allowable dambar protrusion shall not cause the lead width to exceed the maximum b dimension by more than 0.08 mm 4. die is facing down after trim/form. 6/30/08 side view e e1 d e a2 a a1 c c 0.10 0.25 l o a2 a a1 c c 0.10 a a see view b c seating plane seating plane seating plane c b pin #1 id 1 6 2 3 5 4 top view view b view a-a
158 8127c?avr?10/09 attiny4/5/9/10 22. errata the revision letters in this section refer to the revision of the corresponding attiny4/5/9/10 device. 22.1 attiny4 22.1.1 rev. d ? esd hbm (esd stm 5.1) level 1000v ? lock bits re-programming 1. esd hbm (esd stm 5.1) level 1000v the device meets esd hbm (esd stm 5.1) level 1000v. problem fix / workaround always use proper esd protection measures (class 1c) when handling integrated circuits before and during assembly. 2. lock bits re-programming attempt to re-program lock bits to present, or lower protection level (tampering attempt), causes erroneously one, random line of flash program memory to get erased. the lock bits will not get changed, as they should not. problem fix / workaround do not attempt to re-program lock bits to present, or lower protection level. 22.1.2 rev. a ? c not sampled. 22.2 attiny5 22.2.1 rev. d ? esd hbm (esd stm 5.1) level 1000v ? lock bits re-programming 1. esd hbm (esd stm 5.1) level 1000v the device meets esd hbm (esd stm 5.1) level 1000v. problem fix / workaround always use proper esd protection measures (class 1c) when handling integrated circuits before and during assembly. 2. lock bits re-programming attempt to re-program lock bits to present, or lower protection level (tampering attempt), causes erroneously one, random line of flash program memory to get erased. the lock bits will not get changed, as they should not. problem fix / workaround do not attempt to re-program lock bits to present, or lower protection level. 22.2.2 rev. a ? c not sampled.
159 8127c?avr?10/09 attiny4/5/9/10 22.3 attiny9 22.3.1 rev. d ? esd hbm (esd stm 5.1) level 1000v ? lock bits re-programming 1. esd hbm (esd stm 5.1) level 1000v the device meets esd hbm (esd stm 5.1) level 1000v. problem fix / workaround always use proper esd protection measures (class 1c) when handling integrated circuits before and during assembly. 2. lock bits re-programming attempt to re-program lock bits to present, or lower protection level (tampering attempt), causes erroneously one, random line of flash program memory to get erased. the lock bits will not get changed, as they should not. problem fix / workaround do not attempt to re-program lock bits to present, or lower protection level. 22.3.2 rev. a ? c not sampled. 22.4 ATTINY10 22.4.1 rev. c ? d ? esd hbm (esd stm 5.1) level 1000v ? lock bits re-programming 1. esd hbm (esd stm 5.1) level 1000v the device meets esd hbm (esd stm 5.1) level 1000v. problem fix / workaround always use proper esd protection measures (class 1c) when handling integrated circuits before and during assembly. 2. lock bits re-programming attempt to re-program lock bits to present, or lower protection level (tampering attempt), causes erroneously one, random line of flash program memory to get erased. the lock bits will not get changed, as they should not. problem fix / workaround do not attempt to re-program lock bits to present, or lower protection level. 22.4.2 rev. a ? b not sampled.
160 8127c?avr?10/09 attiny4/5/9/10 23. datasheet revision history 23.1 rev. 8127c ? 10/09 1. updated values and notes: ? table 16-1 in section 16.2 ?dc characteristics? on page 116 ? table 16-3 in section 16.4 ?clock characteristics? on page 118 ? table 16-6 in section 16.5.2 ?vcc level monitor? on page 119 ? table 16-9 in section 16.8 ?serial programming characteristics? on page 121 2. updated figure 16-1 in section 16.3 ?speed grades? on page 117 3. added typical characteristics figure 17-36 in section 17.2.7 ?analog comparator off- set? on page 140 . also, updated some other plots in typical characteristics. 4. added topside and bottomside marking notes in section 20. ?ordering information? on page 153 , up to page 156 5. added esd errata, see section 22. ?errata? on page 158 6. added lock bits re-programming errata, see section 22. ?errata? on page 158 23.2 rev. 8127b ? 08/09 1. updated document template 2. expanded document to also cover de vices attiny4, attiny5 and attiny9 3. added section: ? ?comparison of attiny4, attiny5, attiny9 and ATTINY10? on page 4 4. updated sections: ? ?adc clock ? clkadc? on page 18 ? ?starting from idle / adc noise reduction / standby mode? on page 20 ? ?adc noise reduction mode? on page 24 ? ?analog to digital converter? on page 25 ? ?smcr ? sleep mode control register? on page 25 ? ?prr ? power reduction register? on page 26 ? ?alternate functions of port b? on page 48 ? ?overview? on page 83 ? ?physical layer of tiny programming interface? on page 96 ? ?overview? on page 107 ? ?adc characteristics (attiny5/10, only)? on page 120 ? ?supply current of i/o modules? on page 122 ? ?register summary? on page 149 ? ?ordering information? on page 153 5. added figure: ? ?using an external programmer for in-system programming via tpi? on page 97 6. updated figure: ? ?data memory map (byte addressing)? on page 15 7. added table: ? ?number of words and pages in the flash (attiny4/5)? on page 109
161 8127c?avr?10/09 attiny4/5/9/10 8. updated tables: ? ?active clock domains and wake-up sources in different sleep modes? on page 23 ? ?reset and interrupt vectors? on page 35 ? ?number of words and pages in the flash (attiny9/10)? on page 109 ? ?signature codes? on page 110 23.3 rev. 8127a ? 04/09 1. initial revision
162 8127c?avr?10/09 attiny4/5/9/10
i 8127c?avr?10/09 attiny4/5/9/10 table of contents features .............. ................ ................ ............... .............. .............. ............ 1 1 pin configurations ... ................ ................ ................. ................ ............... 2 1.1 pin description ..................................................................................................2 2 overview ................ .............. .............. ............... .............. .............. ............ 3 2.1 comparison of attiny4, attiny5, attiny9 and ATTINY10 ...................................4 3 general information ............ .............. ............... .............. .............. ............ 5 3.1 resources .........................................................................................................5 3.2 code examples .................................................................................................5 3.3 data retention ...................................................................................................5 3.4 disclaimer ..........................................................................................................5 4cpu core ............... .............. .............. ............... .............. .............. ............ 6 4.1 architectural overview .......................................................................................6 4.2 alu ? arithmetic logic unit ...............................................................................7 4.3 status register ..................................................................................................7 4.4 general purpose register file ..........................................................................8 4.5 stack pointer .....................................................................................................9 4.6 instruction execution timing .............................................................................9 4.7 reset and interrupt handling ...........................................................................10 4.8 register description ........................................................................................12 5 memories ............... .............. .............. ............... .............. .............. .......... 14 5.1 in-system re-programmable flash prog ram memory ....................................14 5.2 data memory ...................................................................................................14 5.3 i/o memory ......................................................................................................16 6 clock system ........... ................ ................ ................. ................ ............. 17 6.1 clock subsystems ...........................................................................................17 6.2 clock sources .................................................................................................18 6.3 system clock prescaler ..................................................................................19 6.4 starting ............................................................................................................20 6.5 register description ........................................................................................21 7 power management and sleep modes ........... .............. .............. .......... 23 7.1 sleep modes ....................................................................................................23 7.2 power reduction register ...............................................................................24
ii 8127c?avr?10/09 attiny4/5/9/10 7.3 minimizing power consumption ......................................................................24 7.4 register description ........................................................................................25 8 system control and reset .... .............. .............. .............. .............. ........ 27 8.1 resetting the avr ...........................................................................................27 8.2 reset sources .................................................................................................27 8.3 watchdog timer ..............................................................................................30 8.4 register description ........................................................................................32 9 interrupts ............... .............. .............. ............... .............. .............. .......... 35 9.1 interrupt vectors ..............................................................................................35 9.2 external interrupts ...........................................................................................36 9.3 register description ........................................................................................37 10 i/o ports ............... ................ .............. ............... .............. .............. .......... 40 10.1 overview ..........................................................................................................40 10.2 ports as general digital i/o .............................................................................41 10.3 alternate port functions ..................................................................................45 10.4 register description ........................................................................................50 11 16-bit timer/counter0 ......... .............. ............... .............. .............. .......... 52 11.1 features ..........................................................................................................52 11.2 overview ..........................................................................................................52 11.3 clock sources .................................................................................................54 11.4 counter unit ....................................................................................................55 11.5 input capture unit ...........................................................................................57 11.6 output compare units .....................................................................................59 11.7 compare match output unit ............................................................................61 11.8 modes of operation .........................................................................................62 11.9 timer/counter timing diagrams ........... ..........................................................69 11.10 accessing 16-bit registers ..............................................................................71 11.11 register description ........................................................................................73 12 analog comparator ............ .............. ............... .............. .............. .......... 81 12.1 register description ........................................................................................81 13 analog to digital converter ............. ............... .............. .............. .......... 83 13.1 features ..........................................................................................................83 13.2 overview ..........................................................................................................83 13.3 operation .........................................................................................................83
iii 8127c?avr?10/09 attiny4/5/9/10 13.4 starting a conversion ......................................................................................84 13.5 prescaling and conversion timing ..................................................................85 13.6 changing channel ...........................................................................................88 13.7 adc noise canceler .......................................................................................88 13.8 analog input circuitry ......................................................................................89 13.9 noise canceling techniques ...........................................................................90 13.10 adc accuracy definitions ...............................................................................90 13.11 adc conversion result ...................................................................................92 13.12 register description ........................................................................................93 14 programming interface .......... .............. .............. .............. .............. ........ 96 14.1 features ..........................................................................................................96 14.2 overview ..........................................................................................................96 14.3 physical layer of tiny programming in terface ................................................96 14.4 access layer of tiny programming interface ................................................100 14.5 instruction set ................................................................................................102 14.6 accessing the non-volatile memory co ntroller .............................................104 14.7 control and status space register descriptions ..........................................105 15 memory programming ........ .............. ............... .............. .............. ........ 107 15.1 features ........................................................................................................107 15.2 overview ........................................................................................................107 15.3 non-volatile memories ..................................................................................108 15.4 accessing the nvm .......................................................................................111 15.5 self programming ..........................................................................................114 15.6 external programming ...................................................................................114 15.7 register description ......................................................................................115 16 electrical characteristics ... .............. ............... .............. .............. ........ 116 16.1 absolute maximum rating s* .........................................................................116 16.2 dc characteristics .........................................................................................116 16.3 speed grades ...............................................................................................117 16.4 clock characteristics .....................................................................................118 16.5 system and reset characteristics ................................................................119 16.6 analog comparator characteristics ...............................................................120 16.7 adc characteristics (attiny5/10, only ) .........................................................120 16.8 serial programming characteristics ... ...........................................................121 17 typical characteristics ..... .............. .............. .............. .............. ........... 122
iv 8127c?avr?10/09 attiny4/5/9/10 17.1 supply current of i/o modules ......................................................................122 17.2 attiny4/5/9/10 ...............................................................................................123 18 register summary ............ .............. .............. .............. .............. ........... 149 19 instruction set summary ... .............. ............... .............. .............. ........ 151 20 ordering information .......... .............. ............... .............. .............. ........ 153 20.1 attiny4 ..........................................................................................................153 20.2 attiny5 ..........................................................................................................154 20.3 attiny9 ..........................................................................................................155 20.4 ATTINY10 ........................................................................................................156 21 packaging information ....... .............. ............... .............. .............. ........ 157 21.1 6st1 ..............................................................................................................157 22 errata ........... ................ ................ ................. ................ .............. ........... 158 22.1 attiny4 ..........................................................................................................158 22.2 attiny5 ..........................................................................................................158 22.3 attiny9 ..........................................................................................................159 22.4 ATTINY10 ........................................................................................................159 23 datasheet revision history .. ................ ................. ................ ............. 160 23.1 rev. 8127c ? 10/09 .......................................................................................160 23.2 rev. 8127b ? 08/09 .......................................................................................160 23.3 rev. 8127a ? 04/09 .......................................................................................161
v 8127c?avr?10/09 attiny4/5/9/10
8127c?avr?10/09 headquarters international atmel corporation 2325 orchard parkway san jose, ca 95131 usa tel: 1(408) 441-0311 fax: 1(408) 487-2600 atmel asia unit 1-5 & 16, 19/f bea tower, millennium city 5 418 kwun tong road kwun tong, kowloon hong kong tel: (852) 2245-6100 fax: (852) 2722-1369 atmel europe le krebs 8, rue jean-pierre timbaud bp 309 78054 saint-quentin-en- yvelines cedex france tel: (33) 1-30-60-70-00 fax: (33) 1-30-60-71-11 atmel japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel: (81) 3-3523-3551 fax: (81) 3-3523-7581 product contact web site www.atmel.com technical support avr@atmel.com sales contact www.atmel.com/contacts literature requests www.atmel.com/literature disclaimer: the information in this document is provided in connection wi th atmel products. no license, expr ess or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of atmel products. except as set forth in atmel?s terms and condi- tions of sale located on atmel?s web site, atmel assumes no li ability whatsoever and disclaims any express, implied or statutor y warranty relating to its products including , but not limited to, the implied warranty of merchantability, fitness for a particu lar purpose, or non-infringement. in no event shall atmel be liable for any direct, indire ct, consequential, punitive, special or i nciden- tal damages (including, without li mitation, damages for loss of profits, business interruption, or loss of information) arising out of the use or inability to use this document, even if atmel has been advised of the possibility of such damages. atmel makes no representations or warranties with respect to the accuracy or comp leteness of the contents of th is document and reserves the rig ht to make changes to specifications and product descriptions at any time without notice. atmel does not make any commitment to update the information contained her ein. unless specifically provided otherwise, atmel products are not suitable for, and shall not be used in, automotive applications. atmel?s products are not int ended, authorized, or warranted for use as components in applications intended to support or sustain life. ? 2009 atmel corporation. all rights reserved. atmel ? , logo and combinations thereof, avr ? and others are registered trademarks or trade- marks of atmel corporation or its subsidiaries. other terms and product names may be trademarks of others.


▲Up To Search▲   

 
Price & Availability of ATTINY10

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X